You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Make a collectable money system
What is the issue? Include screenshots / videos if possible!
There is an error that says “Attempt to index nil with :WaitForChild()”
What solutions have you tried so far? Did you look for solutions on the Developer Hub? None of those matched my problem.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
script.Parent.Touched:Connect(function(hit)
local playerChar = hit.Parent
local inPlayers = game.Players:GetPlayerFromCharacter(playerChar)
local leaderstats = inPlayers:WaitForChild("leaderstats") --The part where the error occurs
local robux = leaderstats:WaitForChild("Robux")
robux.Value = robux.Value + 25
script.Parent:Destroy()
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
script.Parent.Touched:Connect(function(hit)
local playerChar = hit.Parent
local inPlayers = game.Players:GetPlayerFromCharacter(playerChar)
if not inPlayers or inPlayers.Parent ~= game.Players then return end
local leaderstats = inPlayers:WaitForChild("leaderstats")
local robux = leaderstats:WaitForChild("Robux")
robux.Value = robux.Value + 25
script.Parent:Destroy()
end)
any part.touched events will fire if they touch anything (ie not just player characters), so if you try to GetPlayerFromCharacter(hit.Parent), unless there is a player called Baseplate or Part, it’s likely to return nil.
It’s best to look for the humanoid first to confirm it is actually a player character that touched the part.
script.Parent.Touched:Connect(function(hit)
local playerChar = hit.Parent
if playerChar:FindFirstChildWhichIsA("Humanoid") then
local inPlayers = game.Players:GetPlayerFromCharacter(playerChar)
local leaderstats = inPlayers:WaitForChild("leaderstats")
local robux = leaderstats:WaitForChild("Robux")
robux.Value = robux.Value + 25
script.Parent:Destroy()
end
end)