Help with code error

I have a script that works fine but there is an error that bugs me every time I touch the part can you help me with it?

script.Parent.Touched:Connect(function(hit)
	local player = hit.Parent:FindFirstChild("Humanoid")
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr.leaderstats["Pog Coins"].Value >= 0 then
		wait ()
		script.Disabled = true
		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		plr.leaderstats["Pog Coins"].Value = plr.leaderstats["Pog Coins"].Value +2
		wait (1)
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		script.Disabled = false
	end
end)

image

Because you didnt filter what the part touches. Use if statement. Touch will return any part it touches

but no objects are touching it and its floating above the ground
image

It appears that you have never created the leaderstats instance, as it errors as nil.

I have created a leaderstats though, and the error only happens when I touch it

Get rid of local player = hit.Parent:FindFirstChild(“Humanoid”) and replace it with if hit.Parent:FindFirstChild(“Humanoid”) then local plr = game.Players:GetPlayerFromCharacter(hit.Parent).

1 Like

Try putting a print(hit.Parent.Name) in the code and see what the name of the object that touches it is.

It says its my character thats hitting it.

Can you show us your leaderstats script?

Make sure that your leaderstats are inside your player when testing.

thank you for all of your help it fixed the error

Oh yea no problem. Glad I could help. :slight_smile:

1 Like