Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my timer not work in a game?

Asked by 9 years ago
gui = game.StarterGui.raidgui.raidtimer.raidtime
timevalue = script.Parent.TimeValue
maxtime =  script.Parent.TimeValue.Value
for i = 1, timevalue.Value do
wait(1)
gui.Text = tostring(maxtime-timevalue.Value)
timevalue.Value = timevalue.Value - 1
end

I am using this code to make a timer and it worked when I pressed run but when I entered a game, it seemed like as soon as the server started, the timer started, but when a player enters, the timer stops. What can I do to change this?

Info: I am using StarterGui-ScreenGui-Frame for the Gui the TimeValue is in Workspace along with the script.

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

Everything in StarterGui is cloned into a player's PlayerGui when they enter the game and each time they respawn. Therefore the timer will only change when a player dies.

The way I recommend doing a countdown GUI is to use two scripts. The first script is inside an IntValue in Workspace, and it makes that value count down. Name the value something that nothing else will be named, for this example I'll assume it's named 'count'. The second script is in the GUI. The second script sets the the text of the GUI to the number of the value in Workspace.

Script in the IntValue:

local time = 50

for i = time,0,-1 do
    wait(1)
    script.Parent.Value = i 
end

In case you don't know: for loops have a third, optional argument that determines how much is added to 'i' each time the loop iterates. If we set it to -1, it will effectively count down.

Script in the GUI:

local label = script.Parent
local count = Workspace:WaitForChild("count")

count.Changed:connect(function()
    label.Text = tostring(count.Value)
end)

Hope I helped!

0
Okay I will try this and see if it works. Venerex 5 — 9y
0
I tried that but this time even the GUI was not changed. When I press run scripts, the IntValue changes like its supposed to but the Guis text does not. Venerex 5 — 9y
0
That is because you're testing it by pressing Run, but scripts do not run in StarterGui. They only run in PlayerGui, and to have a PlayerGui you need a player. Try using PlaySolo instead. Perci1 4988 — 9y
0
Okay thanks. Venerex 5 — 9y
View all comments (2 more)
0
THanks for the help it worked! I think I know how to make it start, end and reset on command but ill check back in if i need any help. Venerex 5 — 9y
0
~If you click the Accept Answer button we both get points!~ Perci1 4988 — 9y
Ad

Answer this question