Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why wasn't this finding the Humanoid in Workspace before?

Asked by 8 years ago

Hi, The code you are seeing below is the working piece of code the line which was not working before was the hit = game.Workspace line this code only started to work when I added in the .Player1 before I was just trying game.Workspace:FindFirstChild("Humanoid") which didn't work the error I received was the following -

Workspace.Part.Script:3: attempt to index local 'hit' (a nil value)

I tried making hit local but still no luck. I need to know why it wasn't working as I am experimenting to help me learn Lua better. Thanks in Advance!

function asfasga(hit)
    hit = game.Workspace.Player1:FindFirstChild("Humanoid")
    hit.JumpPower = 100
    hit.WalkSpeed = 60

end

script.Parent.Touched:connect(asfasga)

1 answer

Log in to vote
0
Answered by
soved 69
8 years ago
Edited 8 years ago

Hi, you do realise that the Touched event will basically fire when it touches anything? You probably didn't anchor the part. So then it falls on the ground and BOOM the event fires.

If you want the Part to change a Player's JumpPower and WalkSpeed then do this.

-- ServerScript.

local WS = game:GetService("Workspace")
local PLAYERS =  game:GetService("Players")
local Part = WS:WaitForChild("Part")

Part.Touched:connect(function(Hit)
    local Plr = PLAYERS:GetPlayerFromCharacter(Hit.Parent)

    if Plr then
        -- We now know it's a Player not a NPC.

        local Char = Plr.Character

        if Char then
            local Hum = Char:FindFirstChild("Humanoid")

            if Hum then
                Hum.JumpPower = 100
                Hum.WalkSpeed = 60
            end
        end
    end
end)
0
Hi, soved I tried anchoring the part but still no luck and the code you have put in your answer is a bit complicated can you please simplify it? Cheesepotato999 8 — 8y
Ad

Answer this question