So I’ve had this error on my script, and it’s ‘attempt to index nil with leaderstats’
I’ve read around but none of the posts have been at all helpful, and I was wondering if anyone could help!
My script:
local debounce = false
script.Parent.Touched:Connect(function(hit)
print("Check1")
local player = hit.Parent
local gameplayer = game.Players:GetPlayerFromCharacter(player)
if not debounce then
debounce = true
wait(3) ---- How long till you collect cash (Seconds)
print("Check2")
gameplayer.leaderstats.Money.Value = gameplayer.leaderstats.Money.Value + 4000---- Money to add
print("Check 3")
wait(10)
debounce = false
end
end)
Can anyone tell me where I’ve gone wrong please?
(Leaderstats Script:)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Parent = leaderstats
end)
Check if the Object you’re hitting is actually the Player and not just some random Object
local debounce = false
script.Parent.Touched:Connect(function(hit)
print("Check1")
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not debounce and Player then -- if Debounce is false and player isnt nil
debounce = true
print("Check2")
gameplayer.leaderstats.Money.Value += 4000---- Money to add
task.wait(13)
print("Check 3")
debounce = false
end
end)
What about it? you are only getting the Detected parts Parent, it could be anything, you appear to be getting the Player from its character, but it may not be its character, thats why im suggesting to check if its actually the player
What this will do is check if its actually the Player, when getting the Player’s Character, if an Objects Parent isnt a Player, it will return nil. but here the if statement is checking if the object touching Is a Player or not