Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

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)

1 answer

✓ Accepted answer

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.

3 comments

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.

0

OMG THANKYOU! i had to add another ".Parent" for it to work tho

0

Great!

This discussion is preserved as it was. New answers, comments, and votes are closed.