I've made a script for a collectible that gives you currency when you touch it. It works perfectly except it gives an error every time i enter the game/ test it in studio. (it gives me the error twice for 2 specific collectibles which i find weird)
Workspace.Worlds.World6.Ticket.TixScript:9: attempt to index nil with 'leaderstats'
The script:
db = false
function tixPickup(partTouching)
if partTouching.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(partTouching.Parent)
if db == false then
db = true
print("Tix Picked Up")
player.leaderstats.Tix.Value = player.leaderstats.Tix.Value + 1
script.Parent.Transparency = 1
wait(10)
script.Parent.Transparency = 0
db = false
end
end
end
script.Parent.Touched:Connect(tixPickup)
I've used a yt tutorial for this but i can't remember which one

2 comments
Why are you setting db to true in the if statement only to set it to false at the end?
Its called debounce, its to prevent a function from being run while it is already running see this https://developer.roblox.com/en-us/articles/Debounce