Setting IntValue to 0 not working

Hello. I’m making a counter for how long the server has gotten without a death. I’ve managed to make the counter but if a player dies it doesn’t reset. How come?

local text = script.Parent.PlayerName
local value = game.ReplicatedStorage.MinutesSinceAccident
while true do
	task.wait(1)
	value.Value = value.Value + 1
	text.Text = value.Value
end

local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		player.Character.Humanoid.Died:Connect(function()
			value.Value = 0
			
		end)
	end)
end)

This is a server script. Seems like it doesn’t run anything after detecting a player dying.

Any help is much appreciated.

Do you want the value to be stored just for the local player or for all the server?, becasue replicated storage its just for the client so i guess when the value changes it changes for the server or something like that.

Anyways here is another code you can use,

local text = script.Parent.PlayerName
local value = game.ReplicatedStorage.MinutesSinceAccident
while task.wait(1) do
	value.Value += 1
	text.Text = tostring(value.Value)
end

local Players = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
			local humanoid = character:WaitForChild("Humanoid")

			humanoid.Died:Connect(function(died)
			value.Value = 0
           end)
	end)
end)

The loop yields the script until it is terminated, and that will never happen. You have to move it at the end of the script so it doesn’t prevent the connection from being created.

The way I would do it is:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local text = script.Parent.PlayerName
local minutesSinceAccident = ReplicatedStorage.MinutesSinceAccident
-- ^ misleading name? it's not minutes but rather seconds, consider renaming

local function updateText()
	text.Text = minutesSinceAccident.Value
end

local function onDied()
	minutesSinceAccident.Value = 0
	updateText()
end

local function onCharacterAdded(character: Model)
	local humanoid = character:WaitForChild("Humanoid", 5)
	if not humanoid then
		return
	end

	humanoid.Died:Once(onDied)
end

local function onPlayerAdded(player: Player)
	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

while task.wait(1) do
	minutesSinceAccident.Value += 1
	updateText()
end
2 Likes

You’re yielding the script, code after the loop will never run. Here’s a cleaner implementation:

--Script in StarterPlayer.StarterCharacterScripts
local Humanoid = script.Parent:WaitForChild("Humanoid")
local value = game.ReplicatedStorage:WaitForChild("MinutesSinceAccident")

Humanoid.Died:Connect(function() value.Value = 0 end)
--Script in script.Parent(according to your original script)
local text = script.Parent:WaitForChild("PlayerName")
local value = game.ReplicatedStorage:WaitForChild("MinutesSinceAccident")

while task.wait(1) do 
	value.Value += 1
	text.Text = value.Value
end
2 Likes

Sorry for the late reply and thank you for all your answers. The reason I set the timer to seconds while the name was “MinutesSinceAccident” was for testing purposes, I wouldn’t want to wait a minute just to test something that didn’t work, lol. Have a good day everyone.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.