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

Why does the Spree leaderstat not get reset?

I have a short script here, it's meant to reset the "Spree" leaderstat of a player to 0 when it dies. It doesn't reset.

PPS = game:GetService("PointsService")
game.Players.PlayerAdded:connect(function(player)
	counter = Instance.new("NumberValue",player)
	counter.Name = "Counter"
	counter.Changed:connect(function(item)
		if counter.Value >= 10 then
			counter.Value = 0
			player.leaderstats.FPoints.Value = player.leaderstats.FPoints.Value +1
			PPS:AwardPoints(player.userId,1)
		end
	end)
	while wait() do if player.Character ~= nil then break end end
	player.Character.Humanoid.Died:connect(function() counter.Value = 0 player.leaderstats.Spree.Value = 0 end)
end)

1 answer

✓ Accepted answer

You are only getting the first character. Once the character dies and another character is created, it doesn't care about the new one. Connect to the character with player.CharacterAdded

local PPS=game:GetService("PointsService")
game.Players.PlayerAdded:connect(function(player)
	local counter=Instance.new("NumberValue",player)
	counter.Name="Counter"
	counter.Changed:connect(function(item)
		if counter.Value>=10 then
			counter.Value=0
			player.leaderstats.FPoints.Value=player.leaderstats.FPoints.Value+1
			PPS:AwardPoints(player.UserId,1)
		end
	end)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function()
			counter.Value=0
			player.leaderstats.Spree.Value=0
		end)
	end)
end)

It also seems that you're using leaderstats and its elements but not creating them in this PlayerAdded connection. I recommend only using one connection to PlayerAdded.

1 comment

0

So this script will reset the stat to zero no matter how many deaths occur? Also, This script is a separate script from the script which includes creating leaderstats.

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