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

Countdown GUI for minigame script - NumberValue not in sync with GUI's text?

Asked by 6 years ago

I've been trying to make a minigame kit for me and other people to use, and I thought it would be a good way to practice scripting since I'm a beginner. I've gotten down the times (like how long each round lasts, and intermission) and what I'm working on now is the count down GUI.

-- K E Y ~  [CE: Can edit] - [DE: Don't edit] - [CD: Can delete]


--[Starter Variables]
local MaxRoundTimeInSeconds = 10 --The maximum round time (in seconds) until intermission. [CE]
local IntermissionTimeInSeconds = 5 -- The intermission time (in seconds) until round begins. [CE]
local RoundStartTime  -- Is set depending on when the round begins. [DE]
local IntermissionStartTime -- Is set depending on when the intermission begins. [DE]


--[Function that defines the beginning of the round]
local function initialize() -- Function name "initialize" to represent beginning of round. [DE]
    RoundStartTime = tick() -- The round start time is set by the current time. [DE]
end

--[Function that defines beginning of intermission]
local function initialize2()
    IntermissionStartTime = tick()
end


--[Functions & Variables for your round]
local function YourFunction() -- This is the name of your function! [CE/CD]
    local YourVariable = ("This is a string!") -- This is a variable that holds a string value, but you can change that. [CE/CD]
    print (YourVariable) -- This is what your function does. [CE/CD]
end


--[The round in action]
while true do -- "while true do" creates a loop. [DE]
    initialize() -- "initialize" function is fired. [DE]
        repeat -- Tells to repeat whatever is below until something stops it. [DE]
            local currentTime=tick() -- The variable, "currentTime", is created.[DE]
            local TimeSinceGameStarted= currentTime - RoundStartTime -- Defines "TimeSinceGameStarted" as the current time subtracted by the time when the round started. [DE]


            --[GUI in action]
            local countdown = game.StarterGui.Timer.Countdown
            countdown.Value.Value = MaxRoundTimeInSeconds
            for i=1, MaxRoundTimeInSeconds do
                countdown.Value.Value = countdown.Value.Value - 1
                countdown.Text = countdown.Value.Value
                wait(1)
            end


            --[Functions fired in round]
            YourFunction() -- This fires the function you created. Rename this if you renamed the previous "YourFunction". [CE]


            wait(0.25) -- Wait one fourth of a second before repeating again.
        until TimeSinceGameStarted > MaxRoundTimeInSeconds -- Repeat until the max round time in seconds is reached...
        wait(IntermissionTimeInSeconds) --... Wait the intermission time that was set.
    end 

(The comments is for people who use it once I'm done making the kit)

The "GUI in action" section is the place where you should focus. I don't get what's wrong with it. When I test the game out, the gui's text says "8" and stays that way, as the value that's inside that gui counts down starting at 0, going into the negatives. Can someone try to figure out what I did wrong? I tried many ways and none of them seems to work!

0
Try checking if the Text is the same as the numberValue then continue counting. thesit123 509 — 6y
0
Thank you for making this kit by the way. Not many people go out of the way to help the community like this, and these help people who can't script a lot! martingalethreat 81 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Your code is not changing the timer on the player's screen. It is changing the timer located in game.StarterGui.

If you want to change the one of the player's screen you'll need to do

plr.PlayerGui.Timer.Countdown.Text = numberValue

Remember that when a player spawns in all of the contents of StarterGui are cloned into each their own personal PlayerGui found in game.Players.MartingaleThreat.PlayerGui, and every time the character respawns a new one from StarterGui is cloned and placed in PlayerGui once again.

So what you'll want to do to fix this is: for i=1, MaxRoundTimeInSeconds do
for _, plr in pairs(game:GetService("Players"):GetPlayers()) do plr.PlayerGui.Countdown.Value.Value = plr.PlayerGui.Countdown.Value.Value - 1 plr.PlayerGui.Countdown.Text = plr.PlayerGui.Countdown.Value.Value end

starterCountdown.Value.Value = starterCountdown.Value.Value - 1
starterCountdown.Text = starterCountdown.Value.Value

wait(1)

end

So we are changing the time of every PlayerGui in the game and changing the time of the countdown in the StarterGui so that when you spawn you get the updated time.

0
So, I tried what you said and since you located "Countdown" wrong (since its parent is a ScreenGui called "Timer") I changed it just by doing plr.PlayerGui.Timer.Countdown.Text. So, I runned the game, and the output says, "Timer is not a valid member of PlayerGui" when it obviously was(I checked the PlayerGui while the game was running and it was there). Do you know why it didn't work? VeryRaven 85 — 6y
0
Hi, VeryRaven, The reason why It is saying 'Not a parent of 'PlayerGui'' is because you're not giving it enough time for it to know that it is in there, after the: for i=1, MaxRoundTimeInSeconds do | you wanna place in a wait(0.5) which will give it some time to know that the countdown GUI is in the actual PlayerGui. xEiffel 280 — 6y
0
You could do that or just do plr.PlayerGui:WaitForChild("Timer") martingalethreat 81 — 6y
Ad

Answer this question