Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

lua - Attempt to index nil with "Touched"?

Here's the deal, I'm incorporating a teleportation system with a tool on 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. The output error is:

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. 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. It should remove the BodyVelocity allowing the player to roam around without having weird gravity.

But the function that's supposed to detect when a player touches the ground isn't working. And I, unfortunately, can't seem to solve the problem.

question from:https://stackoverflow.com/questions/65648117/attempt-to-index-nil-with-touched

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. So, when you call char.Touched:Connect(...) it throws an error because char is 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. 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.

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 :

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...