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

how can i change a different leaderstat when the other one is at a certain point?

i have a script which takes away 1 life ("Lives" is a leaderstat in my game) when a player dies, but i need to make it revert the players stage back to 1 and give them 25 lives again when their Lives are at 0. heres the code i tried (in serverscriptservice)

--variables
local player = game.Players.LocalPlayer
local lives = player.leaderstats.Lives
local stage = player.leaderstats.Stage

if player.lives.Value < 1  then
	lives.Value = lives.Value + 25
	stage.Value = 1
end

thanks

1 comment

0

Your if statement is fine, but you need to run that check every time the lives are updated, currently it's only being checked once when the script runs then never again. Also, LocalPlayer doesn't exist on the server. If something isn't working you should look for errors (red text) in output. You might also be interested in checking out the PlayerAdded event.

1 answer

✓ Accepted answer

You can't use LocalPlayer in ServerScripts, as they don't exist. Also, you can use the Changed function to check whether their lives reduce, and then set their stage back to one.

local Players = game:GetService("Players") 
Players.PlayerAdded:Connect(function(plr)
	local leaderstats = plr:WaitForChild('leaderstats') 
	local Lives = leaderstats:FindFirstChild('Lives')
	local Stage = leaderstats:FindFirstChild('Stage') 

	Lives.Changed:Connect(function()
		if Lives.Value <= 0 then
			Stage.Value = 1
		end
	end) 
end) 

Try that. I'm on mobile so I'm not too sure it will work.

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