Stop counting when dead

So im trying to make something like a “Time alive counter” and this was all i could do with my knowledge of scripting and i need to make it so that it stops going on when the player dies and not restart or just go on with the counting. Ive been trying to find the solution for 3 hours or so.

Ekran görüntüsü 2024-04-28 170206
Ekran görüntüsü 2024-04-28 170210

Im more experience with modelling than scripting so even if this might be a super basic thing it will help a ton.

3 Likes
1 Like

You’d want to be using a localscript first, and check out the docs for more help.

If you’re looking for an instant answer, I haven’t checked or tested this but here’s some code:

-- Get the Player and their Character
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

-- Variables to track time
local startTime = nil
local aliveTime = 0

-- Function to update alive time
local function updateAliveTime()
    if startTime then
        aliveTime = aliveTime + (tick() - startTime)
        startTime = tick()
    end
end

-- Update alive time every frame
game:GetService("RunService").RenderStepped:Connect(updateAliveTime)

-- Start the timer when the character spawns
character.Humanoid.Died:Connect(function()
    startTime = nil
    aliveTime = 0
end)

-- Print alive time every second (optional)
while true do
    wait(1)
    print("Alive Time: " .. aliveTime)
end

Source: OpenAI

1 Like

Alright ill try that real quick. Edit: doesnt work sadly

Fixed version of that basically

local x = 0
local counting = true

while counting and x < 10000000000000000000000000000000000000000 do
    wait(1)
    x = x + 1
end

script.Parent.Text = tostring(x)

In this version, the variable x is incremented by 1 within the while loop using x = x + 1. The loop will continue as long as counting is true and x is less than the specified large number.

2 Likes

it just directly stopped counting with this

has to be in the loop to update

1 Like

If you want the counting to continue indefinitely, you can remove the condition from the while loop. Here’s an example:

local x = 0
local counting = true

while counting do
    wait(1)
    x = x + 1
end

script.Parent.Text = tostring(x)

Only stops if you stop or false set

1 Like

how is that. I dont really know the terms much

1 Like

count up in a seperate thread and then connect some death and spawn events to start and stop the timer

a thread runs seperately from the rest of the script almost like its in a different script, so any wait() inside of it doesnt effect the rest

local count = 0
local counting = true

task.spawn(function()

  while true do
    if counting == true then count += 1 end
    task.wait(1)
  end

end)

local plr = game.Players.LocalPlayer -- this only work in a local script

--when dies
plr.Character.Humanoid.Died:Connect(function())
  counting = false
end

--when they spawn
plr.CharacterAdded:Connect(function()
  counting = true
end
1 Like

Couple of issues with that too

  1. Syntax error: The line count += 1 should be count = count + 1. The += operator is not supported in Lua.

  2. Missing closing parentheses: The examplePlr.CharacterAdded:Connect(function() statement is missing a closing parenthesis.

Here’s the corrected code:

local count = 0
local counting = true

task.spawn(function()
  while true do
    if counting == true then
      count = count + 1
    end
    task.wait(1)
  end
end)

-- When the player dies
examplePlr.Character.Humanoid.Died:Connect(function()
  counting = false
end)

-- When the player spawns
examplePlr.CharacterAdded:Connect(function()
  counting = true
end)
1 Like

do i use a normal script or a local script

did u use ai for that

the parenthesis is closed at the end) because :Connect is a method that encloses that entire function

also += is not supported in lua, but is added in roblox luau

Normal script should be for the server side if you want

no it should be in a local script because its a seperate one for each player and its inside of a gui

Or count = count +1 has the same result too

if you know it has the same result then why did you say it was a syntax error

I thought it was the value of something

Or increment count variable in the context

the upper one is from another script not being in place dont mind that one. the other red line is the thing