Is there something wrong with my script?

Hello! I’m new to scripting and I need some of your help.

local Coins = game.Players.LocalPlayer:WaitForChild("leaderstats").Money

if Coins.Value < 0 then Coins.Value = 2 -- Should be giving the player money if it's below 0 --

end

This code doesn’t work. It just gives me nothing. Doesn’t even print.
It’s in starterplayerscripts btw
p.s i added a print to see if it’s actually working but it doesn’t print

You should probably check if Coins.Value is equal to 0, not below 0.

if Coins.Value == 0 then
    Coins.Value = 2
end

Also, you shouldn’t change leaderstats in a local script, because then the server won’t see/detect the change!

local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local Money = leaderstats:WaitForChild("Money")

if Money.Value <= 0 then
	Money.Value = 2
end

For some reason it isn’t giving me money, nor printing, no error, i made it as a normal script now, it’s already set to 0 btw

Needs to be a local script because “game.Players.LocalPlayer” isn’t defined in server scripts.

You should probably add this to your leaderstats script if you want the stat change to replicate to the server. Could you provide it?

local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local Money = leaderstats:WaitForChild("Money")


if Money.Value <= 0 then
	Money.Value = 2
end

If you don’t care about replication then this will work as a local script inside StarterPlayer/StarterCharacter scripts.

Where did you place this print on top of everything? if so are you sure the script is not disabled

it’s working now! thanks, but it’s still going under 0. Should I make a loop or something?

1 Like
local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local Money = leaderstats:WaitForChild("Money")

while task.wait() do
	if Money.Value <= 0 then
		Money.Value = 2
	end
end

There’s your loop.

1 Like

well, i added it in the middle of the function so yeah, and no it was not disabled

Just in case you want a server-sided version of the script:

--In ServerScriptService
game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = plr:WaitForChild("leaderstats")
    local Coins = leaderstats:WaitForChild("Money")
    while Coins.Value <= 0 do
	    Coins.Value = 2
    end
end)