[英]Attempt to index nil with “Touched”?
Here's the deal, I'm incorporating a teleportation system with a tool on Roblox Studio.这是交易,我正在将一个传送系统与 Roblox Studio 上的一个工具结合起来。 However, this error has got me stumped.
然而,这个错误让我很难过。 I will provide the code and other useful sources below and describe the issue after.
我将在下面提供代码和其他有用的资源,并在之后描述问题。
local tool = script.Parent.Parent
local debounce = false
local afterTeleport = false
local plrName = game.Players.LocalPlayer.Name
local char = workspace:FindFirstChild(plrName)
tool.Activated:connect(function(m)
if debounce == false then
local hum = game.Players.LocalPlayer.Character.Humanoid
local anim_feet = hum:LoadAnimation(script.Parent.Animation)
local current = anim_feet
local bodyVelocity = tool.Handle.LocalScript.BodyVelocity:Clone()
debounce = true
current:Play()
wait(1.1)
local gui = script.Parent.TransitionGui:Clone()
-- Properties for UI
gui.Parent = game.Players.LocalPlayer.PlayerGui
-- Transition
gui.Frame.BackgroundTransparency = 1
wait(0.02)
gui.Frame.BackgroundTransparency = 0.8
wait(0.02)
gui.Frame.BackgroundTransparency = 0.6
wait(0.02)
gui.Frame.BackgroundTransparency = 0.4
wait(0.02)
gui.Frame.BackgroundTransparency = 0.2
wait(0.02)
gui.Frame.BackgroundTransparency = 0
-- Teleport Player Into Sky Above Them
hum.Parent.HumanoidRootPart.CFrame =
CFrame.new(Vector3.new(hum.Parent.HumanoidRootPart.CFrame.X, hum.Parent.HumanoidRootPart.CFrame.Y + 700, hum.Parent.HumanoidRootPart.CFrame.Z))
bodyVelocity.Parent = hum.Parent.HumanoidRootPart
afterTeleport = true
wait(0.02)
gui.Frame.BackgroundTransparency = 0.2
wait(0.02)
gui.Frame.BackgroundTransparency = 0.4
wait(0.02)
gui.Frame.BackgroundTransparency = 0.6
wait(0.02)
gui.Frame.BackgroundTransparency = 0.8
wait(0.02)
gui.Frame.BackgroundTransparency = 1
wait(0.02)
end
end)
char.Touched:Connect(function(interact)
local hum = game.Players.LocalPlayer.Character.Humanoid
if afterTeleport == true then
if interact:IsA("Terrain") then
if hum.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
hum.Parent.HumanoidRootPart.BodyVelocity:Destroy()
end
elseif interact:IsA("Part") then
if hum.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
hum.Parent.HumanoidRootPart.BodyVelocity:Destroy()
end
elseif interact:IsA("MeshPart") then
if hum.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
hum.Parent.HumanoidRootPart.BodyVelocity:Destroy()
end
end
end
end)
To me, this seems correct.对我来说,这似乎是正确的。 But the issue is whenever I play-test the code, the output displays an error that I don't understand.
但问题是每当我对代码进行测试时,output 都会显示我不理解的错误。 The output error is:
output 错误是:
16:35:59.453 Players.BigBetterBuilder.Backpack.Sky Port.Handle.LocalScript:58: attempt to index nil with 'Touched'
My goal for this tool is that when it is activated, it will teleport you into the sky 700 studs above the players' current position.我对这个工具的目标是,当它被激活时,它将把你传送到玩家当前 position 上方 700 处的天空中。 It will add a BodyVelocity to the player's HumanoidRootPart causing the player to slow down the decent speed, and when the player touched the ground.
它将向玩家的 HumanoidRootPart 添加一个 BodyVelocity ,导致玩家减慢适当的速度,以及当玩家接触地面时。 It should remove the BodyVelocity allowing the player to roam around without having weird gravity.
它应该移除 BodyVelocity 允许玩家在没有奇怪重力的情况下四处漫游。
But the function that's supposed to detect when a player touches the ground isn't working.但是应该检测玩家何时接触地面的 function 不起作用。 And I, unfortunately, can't seem to solve the problem.
不幸的是,我似乎无法解决问题。
You've got a timing issue in your code.您的代码中存在时间问题。 At the top of your script, you are trying to access the player's character model, but when this script runs, that character might not have been loaded into the workspace yet.
在脚本的顶部,您尝试访问玩家角色 model,但是当此脚本运行时,该角色可能尚未加载到工作区中。 So, when you call
char.Touched:Connect(...)
it throws an error because char
is null.因此,当您调用
char.Touched:Connect(...)
时,它会引发错误,因为char
是 null。
But you've got a different problem as well.但是你也有一个不同的问题。 The player's character is a Model, and Model's don't have a Touched event like Parts do.
玩家的角色是 Model,而模型没有像 Parts 那样的 Touched 事件。 So in order to use the Touched event, you either need to attach it to platforms that a character might touch, or to the Parts inside the character model.
因此,为了使用 Touched 事件,您需要将其附加到角色可能触摸的平台,或角色 model 内部的部件。
So try moving the char.Touched
connection inside a callback that fires once the player and character have properly loaded into the game, and attach the Touched connection instead to the different Parts in the character model:因此,尝试将
char.Touched
连接移动到一旦玩家和角色正确加载到游戏中就会触发的回调中,并将 Touched 连接附加到角色 model 的不同部分:
local player = game.Players.LocalPlayer
player.CharacterAdded:Connect(function(character)
-- create a helper function for checking if we're touching the ground
local function checkTouchedGround(interact)
if not afterTeleport then
return
end
local shouldRemoveVelocity = false
if interact:IsA("Terrain") then
shouldRemoveVelocity = true
elseif interact:IsA("Part") then
shouldRemoveVelocity = true
elseif interact:IsA("MeshPart") then
shouldRemoveVelocity = true
end
if shouldRemoveVelocity then
local bv = character.HumanoidRootPart:FindFirstChild("BodyVelocity", 0.1)
if bv then
bv:Destroy()
end
end
end
-- listen for any time any player parts touch the ground
for _, child in ipairs(character:GetDescendants()) do
if child:isA("BasePart") then
child.Touched:Connect(checkTouchedGround)
end
end
end)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.