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

"Script:5: attempt to index nil with 'WaitForChild' ?

Asked by
hot_spi 20
4 years ago
Edited 4 years ago

i tried to make a script where if you touch the part you'll have +50 cash and get teleported back where you started. But when i tested it out it says"Script:5: attempt to index nil with 'WaitForChild' ".Here's the script:

local TPpart = script.Parent

TPpart.Touched:Connect(function(h)
    local player = game.Players.LocalPlayer
    local Cash = player:WaitForChild("leaderstats").Cash
    Cash.Value = Cash.Value + 50
    local humanoid = h.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil then
        h.Parent.HumanRootPart.CFrame = CFrame.new(script.Parent["TeleportPart"].Position)
    end

end)

It would be really nice for someone to explain why this happened and how to fix it :D

0
first of all, the way you defined the player is incorrect. you can not get the localplayer from a server script. the second thing i noticed is that you wrote "HumanRootPart" instead of "HumanoidRootPart" (Line 9) LennyPlayzYT 269 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hello hot_spi!

Read the comment about your script from me.

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    player.leaderstats.Cash.Value + player.leaderstats.Cash.Value + 50
    player.Character:SetPrimaryPartCFrame(CFrame.new(script.Parent.TeleportPart.Position))
end)

Hope I helped, LennyPlayzYT.

0
*also for some reason on my computer at least, the end brackets do not show for some weird reason (talking about line 4). if this is happening to you as well, hover over the script and click on view source, then the whole code should be there. LennyPlayzYT 269 — 4y
0
OMG THANKYOU! i had to add another ".Parent" for it to work tho hot_spi 20 — 4y
0
Great! LennyPlayzYT 269 — 4y
Ad

Answer this question