generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: This while variable == true do loop isn't working?

Previous Thread :: Next Thread 
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
07 Jan 2014 04:46 PM
Ok so before you go TL;DR, I cut out as much as I can. You can just skim through the minigame to get an idea.

The problem here is I first say the variable ClockRunning is false, then stuff happens in inside a function I say ClockRunning is true. But a loop that says while ClockRunning == true do doesn't start up...






local blue = 0
local GameEnded = true
local GameRunning = script.GameRunning.Value
local ClockRunning = false
script.Minutes.Value = 3
script.Seconds.Value = 0

--------------------------------------------------------
-- Timer Stuffs
while ClockRunning == true do
wait(1)
script.Seconds.Value = script.Seconds.Value - 1
if script.Seconds.Value < 0 then
script.Seconds.Value = 59
script.Minutes.Value = script.Minutes.Value - 1
end
end
--------------------------------------------------------
-- End the Game
function EndGame()
ClockRunning = false
script.Minutes.Value = 0
script.Seconds.Value = 0
-- something that kills the players and changes their team
wait(8)
showMessage(getWinningTeamString()) -- showMessage is a function that shows a gui
wait(5)
hideMessage() -- take a guess
GameEnded = true -- This is exactly like the while clockrunning loop... but this one works
end
--------------------------------------------------------
-- Runthrough of Game
function RunGame()
for i=1,36 do
print("Checking Players")
CheckPlayers()
wait(5)
if blue == 0 then
EndGame()
break
end
end
if blue ~= 0 then
EndGame()
end
end
--------------------------------------------------------
-- The Actual Starting of the Game
function startGame()
if GameRunning == true then
wait(2)
-- move all players to blue team, pick one and move him to green, kill everyone.
wait(8)
showMessage("Zombies have 3 minutes to infect all Humans.")
script.Minutes.Value = 3
script.Seconds.Value = 0
wait(5)
ClockRunning = true --[[The while ClockRunning == true do loop doesn't start even though I said this]]
hideMessage()
RunGame()
end
end
--------------------------------------------------------
while GameEnded == true do -- This one works but the clockrunning one doesn't?
--blah blah, make GameRunning true and GameEnded false
end
Report Abuse
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
07 Jan 2014 05:10 PM
Could anyone skim through it and see if it's some silly mistake or something. I can't find anything
Report Abuse
PiggyJingles is not online. PiggyJingles
Joined: 13 Jan 2009
Total Posts: 2472
07 Jan 2014 05:13 PM
What is the problem. If you don't know, narrow down the lines with prints.

Btw
while variable do works aswell.
Report Abuse
wubbzy301 is not online. wubbzy301
Joined: 15 May 2010
Total Posts: 1188
07 Jan 2014 05:14 PM
Can you give me a error on the output?
Example:
script.Parent.Script - Line 1337
Report Abuse
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
07 Jan 2014 05:16 PM
No error, and I tried using prints. it simply doesn't want to start the clock. :/
Report Abuse
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
07 Jan 2014 05:54 PM
bump
Report Abuse
robotmega is not online. robotmega
Joined: 16 May 2009
Total Posts: 14084
07 Jan 2014 07:22 PM
3k robux
Report Abuse
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
07 Jan 2014 09:32 PM
I'm sorry what?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
07 Jan 2014 09:35 PM
You're most likely problem is that the loop is that when you use:
"while condition do" and the condition becomes false, the loop stops. When that condition becomes true (which it is false in the beginning) then nothing happens, I'm not sure if you thought it's going to start the loop when the variable becomes true
Report Abuse
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
07 Jan 2014 09:38 PM
Ok so just remove the local ClockRunning = false in the beginning and change it to like local ClockStopped = true and have it check for while ClockRunning = false do?
Report Abuse
dog456pop248 is not online. dog456pop248
Joined: 24 Aug 2008
Total Posts: 3977
07 Jan 2014 09:39 PM
Try this:

corotuine.resume(coroutine.create(function()
while wait(1) do
if ClockRunning then
script.Seconds.Value = script.Seconds.Value - 1
if script.Seconds.Value < 0 then
script.Seconds.Value = 59
script.Minutes.Value = script.Minutes.Value - 1
end
end
end
end))
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
07 Jan 2014 09:40 PM
It depends, do you want that loop to run when it becomes true?
If so you would have to call a function that does that loop after you set it to true, or wrap an infinite loop in a thread that has a conditional statement to check if it's true.

In example:
coroutine.wrap(function()
while true do
if whatever == true then
--code
end
end
end)()
Report Abuse
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
07 Jan 2014 09:45 PM
Wait is it because I'm trying to run both RunGame() and that clock loop at the same time?
If so I can do a workaround and place it in a different script.
Report Abuse
dog456pop248 is not online. dog456pop248
Joined: 24 Aug 2008
Total Posts: 3977
07 Jan 2014 09:46 PM
Ah, that's where coroutines come in. They allow you to run loops and functions sperate from your code so they don't make it stop working

To make a coroutine without any fancy business, just use:

coroutine.resume(coroutine.create(function()
--code
end))
Report Abuse
dog456pop248 is not online. dog456pop248
Joined: 24 Aug 2008
Total Posts: 3977
07 Jan 2014 09:48 PM
"Ah, that's where coroutines come in. They allow you to run loops and functions separate from your code so they don't make it stop working"

Let me rephrase that,

"Ah, that's where coroutines come in. They allow you to run loops and functions separate from your code so it doesn't get stuck on a loop"
Report Abuse
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
07 Jan 2014 09:48 PM
@dog

Ok can you fit that in with my while ClockRunning == true do loop?
I've never done corountines before
Report Abuse
dog456pop248 is not online. dog456pop248
Joined: 24 Aug 2008
Total Posts: 3977
07 Jan 2014 09:50 PM
Yes, I already did, a couple posts above.

It's EXTREMELY simple to do, you just put your code inside of a coroutine if you don't want to it get stuck. Memorize the heck out of these two lines:

coroutine.resume(coroutine.create(function()
--put yo loop in here
end))
Report Abuse
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
07 Jan 2014 09:53 PM
Gotcha.

So when I say

ClockRunning = true
RunGame()

It will start up that loop and do RunGame at the same time?
Report Abuse
dog456pop248 is not online. dog456pop248
Joined: 24 Aug 2008
Total Posts: 3977
07 Jan 2014 09:54 PM
It should, If you added the coroutine correctly.
Report Abuse
TheInnovative is not online. TheInnovative
Joined: 31 Dec 2008
Total Posts: 23486
08 Jan 2014 03:32 PM
and the clock didn't start... no errors

this is what I have






coroutine.resume(coroutine.create(function()
while ClockRunning == true do
wait(1)
script.Seconds.Value = script.Seconds.Value - 1
if script.Seconds.Value < 0 then
script.Seconds.Value = 59
script.Minutes.Value = script.Minutes.Value - 1
end
end
end))

ClockRunning = true
Report Abuse
dog456pop248 is not online. dog456pop248
Joined: 24 Aug 2008
Total Posts: 3977
09 Jan 2014 10:08 PM

coroutine.resume(coroutine.create(function()
while wait(1) do
if ClockRunning then
script.Seconds.Value = script.Seconds.Value - 1
if script.Seconds.Value < 0 then
script.Seconds.Value = 59
script.Minutes.Value = script.Minutes.Value - 1
end
end
end))

ClockRunning = true


Try that.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image