繁体   English   中英

尝试用“Touched”索引 nil?

[英]Attempt to index nil with “Touched”?

这是交易,我正在将一个传送系统与 Roblox Studio 上的一个工具结合起来。 然而,这个错误让我很难过。 我将在下面提供代码和其他有用的资源,并在之后描述问题。

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)

对我来说,这似乎是正确的。 但问题是每当我对代码进行测试时,output 都会显示我不理解的错误。 output 错误是:

16:35:59.453  Players.BigBetterBuilder.Backpack.Sky Port.Handle.LocalScript:58: attempt to index nil with 'Touched' 

我对这个工具的目标是,当它被激活时,它将把你传送到玩家当前 position 上方 700 处的天空中。 它将向玩家的 HumanoidRootPart 添加一个 BodyVelocity ,导致玩家减慢适当的速度,以及当玩家接触地面时。 应该移除 BodyVelocity 允许玩家在没有奇怪重力的情况下四处漫游。

但是应该检测玩家何时接触地面的 function 不起作用。 不幸的是,我似乎无法解决问题。

您的代码中存在时间问题。 在脚本的顶部,您尝试访问玩家角色 model,但是当此脚本运行时,该角色可能尚未加载到工作区中。 因此,当您调用char.Touched:Connect(...)时,它会引发错误,因为char是 null。

但是你也有一个不同的问题。 玩家的角色是 Model,而模型没有像 Parts 那样的 Touched 事件。 因此,为了使用 Touched 事件,您需要将其附加到角色可能触摸的平台,或角色 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM