Issue with finding if the Leaderstats is 0

Hello! Im having trouble with these scripts;

(script is local script)

local player = game.Players.LocalPlayer

game.Players.PlayerAdded:connect(function(player)
	local Age = player.leaderstats.Age
	if Age == 0 then
		script.Parent.Visible = true
	end
end)

I kinda want it to check if the Leaderstats is Zero, then open a GUI Frame if it is.

And this one I want to add to the stats

(Script is normal script)

script.Parent.MouseButton1Click:connect(function()
    local player = game.Players.LocalPlayer
	player.leaderstats.Age.Value = player.leaderstats.Age.Value + 10
	script.Parent.Parent.Visible = false
end)

On this line, you use if Age == 0 then, but that just checks if the variable is 0, which it isnt. You’ll want to change this to check the value instead, since that’s what actually holds the data: if Age.Value == 0 then

1 Like
game.Players.PlayerAdded:Connect(function(player)
	local Age = player.leaderstats.Age
	if Age.Value == 0 then
		script.Parent.Visible = true
	end
end)
1 Like

PlayerAdded will not be fired because the player is added before the localscripts are created.
And, changing these values on the client-side will not replicate to the server.

Basically, the server will not see that chance, nor will any other players within the game.
Use a server-script instead if you’re going to implement a saving system or want others to see the value.

Thank you! But its still not working, maybe its the type of script im using? Im using a Local script

Here’s a simple script (server-script) that will help you out.

local Players = game:GetService('Players')


local function PlayerAdded(Player)
	-- Code
end


local GetPlayers = Players:GetPlayers()
for i = 1, #GetPlayers do
	local Target = GetPlayers[i]
	coroutine.resume(coroutine.create(function()
		PlayerAdded(Target)
		-- Run this function for each player in parralell.
		-- In rare cases, the player is added before the event is able to pick it up.
	end))
end	
Players.PlayerAdded:Connect(PlayerAdded)

Good luck on your project.

1 Like

Hmmmmm but I can’t add a server-script into GUI frame?

No, place this iside the ServerScriptService service.
Use RemoteEvent:FireClient() to update UI-related things for the player.

1 Like

Ok thank you for all the help!

1 Like

Feel free to message me (or comment here) for further questions.
And — you’re welcome.

1 Like