In my game, I inserted collectable coins, but for some reason, the script is not working and I get the message:
18:10:42.625 - Workspace.Coin.CollectScript:8: attempt to index local 'player' (a nil value)
Does somebody know what this means and how I can fix it?
This is the script inside the coin:
local db = true
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") == nil then
if db == true then
db = false
script.Parent.Transparency = 1
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
script.Sound.Play()
wait(60)
script.Parent.Transparency = 0
db = true
end
end
end)
and this is the script in the workspace:
game.Players.PlayerAdded:Connect(function(plr)
local f = Instance.new("Folder", plr)
f.Name = "leaderstats"
local coins = Instance.new("IntValue", f)
coins.Name = "Coins"
coins.Value = 0
end)

1 comment
Thank you!!!