Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can someone tell me what is wrong with my script? (More details in desc.)

local db = true
local playerstats = game.Players:FindFirstChild("leaderstats")


script.Parent.ClickDetector.MouseClick:connect(function(onClicked)

if db == true then
	db = false
	script.Parent.Transparency = 1
	playerstats.Robux.Value = playerstats.Robux.Value + 10
	script.Parent.Sound:Play()
	script.Parent.ParticleEmitter.Enabled = false
	wait(10)	
	db = true
	script.Parent.Transparency = 0
	script.Parent.ParticleEmitter.Enabled = true
	
end
end)

I tested this script and it said this in the output:

"16:41:40.424 - Workspace.Soda Can.CollectScript:10: attempt to index a nil value"

I've tried multiple things to fix such as changing up the variable, and nothing seems to work. Any help would be greatly appreciated.

2 comments

0

You Are Looking In Players For 'playerstats' which is not Players

0

i just named the variable that, does it really matter?

1 answer

✓ Accepted answer

Why are you trying to find "leaderstats" in Players? It obviously errors because "leaderstats" does not exist under Players. (assuming that leaderstats is an IntValue)

Here is the correct way of your code:

local db = true

script.Parent.ClickDetector.MouseClick:Connect(function(Player)

local playerstats = Player:FindFirstChild("leaderstats")

if db == true then
    db = false
    script.Parent.Transparency = 1
    playerstats.Robux.Value = playerstats.Robux.Value + 10
    script.Parent.Sound:Play()
    script.Parent.ParticleEmitter.Enabled = false
    wait(10)    
    db = true
    script.Parent.Transparency = 0
    script.Parent.ParticleEmitter.Enabled = true

end
end)

2 comments

0

Please upvote this if this helped.

0

thanks this fixed it :)

This discussion is preserved as it was. New answers, comments, and votes are closed.