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

How do I make a countdown GUI when a part is clicked?

I'm trying to make the countdown from the hunger games but nothing happens from the script, but output says everything is fine.

local player = game.Players:children() 
function onMouseClick(player)
	wait(40)
	game.StarterGui.CountdownGui.TextButton.Visible = true
local time = 60  
for i = 1, 60 do  
wait(1) 
time = time - 1 
workspace.Counter:Play()
script.Parent.Parent.Parent.StarterGui.CountdownGui.TextButton.Text = tostring(time) 
wait(60)
script.Parent.Parent.Parent.StarterGui.CountdownGui.TextButton.Visible = false
workspace.Cannon:Play()
wait(.1)
workspace.Bloodbath:Play()
player.human.WalkSpeed = (16)
end 
end

I'm a bit of a noob, so please go easy on me!

3 answers

local start = 60
local finish = 0

function COUNTDOWN()
	for i = start, finish, -1 do -- decrease 'start' by 1 every second
		print(i) -- progress
		workspace.Counter:Play()
		wait(1)
	end

	print("countdown finished!")
end

function CLICKED()
	COUNTDOWN()
	-- rest of your code here
end

1 comment

0

note that any code below the for i loop will be paused until the loop is completed. No need to add a wait(60)

you are using game.startergui, this dont will work, you need select playergui, and not startergui, as you need to specify where is the player gui, try this :

script.Parent.ClickDetector.MouseClick:Connect(function(onClick)
    game.Players.LocalPlayer .PlayerGui.CountdownGui.TextButton.Visible = true
    wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 60"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 59"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 58"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 57"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 56"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 55"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 54"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 53"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 52"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 51"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 50"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 49"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 48"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 47"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 46"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 45"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 44"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 43"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 42"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 41"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 40"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 39"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 38"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 37"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 36"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 35"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 34"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 33"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 32"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 31"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 30"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 29"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 28"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 27"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 26"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 25"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 24"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 23"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 22"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 21"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 20"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 19"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 18"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 17"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 16"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 15"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 14"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 13"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 12"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 11"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 10"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 09"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 08"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 07"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 06"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 05"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 04"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 03"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 02"
wait(1)
   game.Players.LocalPlayer.CountdownGui.TextButton.Text = "00 : 01"
wait(1)
-- put here what you wanna do--
end)

Make sure this script is a script and that it is in the brick with a ClickDetector

2 comments

0

Your code is inefficient. You could simplify this to a few lines of code

0

^ I agree. This is such a waste of time & space.

Read the paragraph below. This will explain a term that you might not know:

When I use the word method, it is another word for function. You will see the term method in other programming languages that are not LUA.

Alright, know on to the fixing.

I see a few problems in your script

First, the method, :children(), is deprecated. Roblox recommends to not use deprecated methods and syntaxes.

Next, you didn't include a click detector in your code. This is very essential for the click event. An event is a function that fires when something happens.

On line 6, you used a for loop. You could use this, however, a while loop uses the variable time in the condition (i.e in while true do, true is the condition; in while 5 == 5 do, 5 == 5 is the condition; the condition has to be true for the loop to fire)

On line 16 in your code, you made a huge mistake.

You have done player.human.WalkSpeed. "human" is not in the player. The correction of this error is player.Character.Humanoid.WalkSpeed.

I stored player.Character in a variable called char.

You might be confused at player.Character or player.CharacterAdded:Wait(). To clear that up, if player.Character is nil,or not existing, it could wait until the character is added(see the method :Wait() ?) After that, it gets the humanoid(:WaitForChild() is another way to access a part; this will wait a few seconds(just in case the object just becomes not nil) until it will finally say the object is nil)

Then, you put 16 in between parenthesis. This is unneeded.

Finally, to make this event work when you click a part, you need to use your click detector that you stored (remember that the click detector is essential for the click event?). We will use an RBXScriptSignal known as Connect to connect the function to the event. In this event, when the part is clicked, the method, onMouseClick, will be fired.

local players = game.Players:GetChildren()  -- :children() is deprecated, use :GetChildren()
local clickDetector = script.Parent:WaitForChild("Your Click Detector Name Here")

function onMouseClick(player)
	local char = player.Character or player.CharacterAdded:Wait() -- gets character
	local humanoid = char:WaitForChild("Humaonid")
	local playerGui = player:WaitForChild("PlayerGui")
	local textButton = playerGui:WaitForChild("CountdownGui"):FindFirstChild("TextButton")
    	local time = 60

	wait(40)
    	playerGui.CountdownGui.TextButton.Visible = true
	
	-- I find a while loop would be better to use here to use the time variable
	while time <= 60 and time >= 0 do 
		wait(1) 
		time = time - 1 
		workspace.Counter:Play()
		textButton.Text = tostring(time) 
	--	wait(60) I will comment this line so it won't wait 60 seconds
		textButton.Visible = false
		workspace.Cannon:Play()
		wait(0.1)
		workspace.Bloodbath:Play()
		 -- there is no need to have parenthesis around the number
		player.Humanoid.WalkSpeed = 16
	end 
end

clickDetector.MouseClick:Connect(onMouseClick)

There two important things that you should know. You don't have to read this:

Please indent your code

Indenting your code makes it look more cleaner. This just makes coding be organized. This could be crucial when you are coming back.

Speaking of coming back to an old script, comment some confusing lines of code. When you come back, you could read what you wrote instead of reading a whole block of code and trying to understand it

Filtering Enabled or Experimental Mode off changes how your game will work. I won't go in depth here, but, this will be very important later on when you are learning. This script in Filtering Enabled might not work because of how Filtering Enabled works

Sorry about all of those coding terms. Sorry if that intimidated you.

3 comments

0

I was hoping for it to be a global GUI and not for a certain player, thanks for helping though. I tested it and nothing exactly happened so I tried to edit it, but still nothing worked. I was also trying to get the counter to repeatedly play with the time going down (I'm not sure if that what it does since I can't get the script to work,) again thanks for helping! 

0

To make it global, you would need to use RemoteEvents.

0

Next, you need to put the click detector in the part. Then, you put the click detector name in the quotation marks in the WaitForChild method in the variable click detector

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