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

Error with while true do loop?

I was attempting to activate and deactivate values to run all the scripts in my game but the loop does'nt seem to run, i was looking to see who could help and solve my issue :D THANKS FOR READING

while true do
local Gamestart = workspace.Gamestart.Value
local Intermission = workspace.Intermission.Value
local Inttime = workspace.Inttime.Value
local Resources = workspace.Resources.Value
wait(30)
Intermission = true
Inttime = Inttime -1
if Inttime == 0  then
Intermission = false
Inttime = 30
wait()
Gamestart = true 
if Resources == 0 then
Gamestart = false
local plrs = game.Players:GetPlayers()

for _, player in pairs(plrs) do
    if player.Character and player.Character:FindFirstChild("Humanoid") then
        player.Character.Humanoid.Health = 0
    end
end
end
end
end

2 comments

0

tauhruiwyaieuyro there are like 13 errors.

0

it doesnt show me any errors :/ weird

2 answers

✓ Accepted answer

You basically had a bunch of the same error in your script. I'll go ahead, and post the fixed script first, then explain it a bit.

Here it is:

while true do
	local Gamestart = workspace.Gamestart
	local Intermission = workspace.Intermission
	local Inttime = workspace.Inttime
	local Resources = workspace.Resources
	Intermission.Value = true
	repeat wait(1) --Repeats following code every second
		if Inttime.Value>0 then
			Inttime.Value=Inttime.Value-1
		end
	until Inttime.Value<=0 --Stop repeating once it hits 0
	if Inttime.Value == 0  then
		Intermission.Value = false
		Inttime.Value = 30
		wait()
		Gamestart.Value = true 
		if Resources.Value == 0 then
			Gamestart.Value = false
			local plrs = game.Players:GetPlayers()
			for _, player in pairs(plrs) do
			    if player.Character and player.Character:FindFirstChild("Humanoid") then
			        player.Character.Humanoid.Health = 0
			    end
			end
		end
	end
end

So basically, the error you had was that you were defining the Variables: Gamestart, Intermission, Inttime, and Resources, as their Values. When you do this, the script stores the variable as what the value of it was at the time it was called.

Here's an example:

local ValueExample=script.Parent.Value --Let's say this comes out as true
script.Parent.Value=not script.Parent.Value --Changes the value to false
print(ValueExample) --Would print out "true"

This would happen because the value is being changed correctly, but as I already stated, it is storing the value, not the object that you need to access to change it's property.

To fix the above script, we would do this:

local ValueExample=script.Parent --Let's say this is true again
script.Parent.Value=not script.Parent.Value --Changes the value to false
print(ValueExample.Value) --Would print out "false" this time

This time we accessed the object, and were able to change it's property correctly, so the script worked out how we wanted it to.


Anyways, I hope this helped you out a bit. If you have any problems/questions, please leave a comment below :P

14 comments

0

Thank You! I have a question, wouldnt the loop need to have :Changed function? or will that not be a issue

0

Glad I could help :P . Anyways, I don't think it should be an issue, but if you have any problems with it, let me know, and I'll see what I can do.

0

I still have no errors, but it wont run :/ any ideas?

0

Nevermind, it runs but it wont count down

0

Just make sure you have all the values correct, and everything. That would be the only problem I would think. Like on line 09, make sure that the value is on 0, or else nothing will happen.

0

Thats not the issue the values number wont go down

0

It goes down once, but whats the proper what to put it to loop down to 0?

0

Oh, ok, i'll update my answer. I assume you want it to count down, 1 each second?

0

Correct! THAT IS THE NUMBER 1 ANSWER :P

0

Lol ok, well I believe it should work now :P

0

Yeah it does, not i need to fix the issue that the text on the gui isnt updating. Scripter Problems.

0

NEW ISSUE, sorry, the gamestart goes to true before the countdown ends, please let me know whats wronge!

0

I'm not sure what would be wrong...make sure it's set to false to begin with.

0

Every thing is false its odd :/

To make it count down you need to make it wait 1 second, and then subtract 1 from the timer value

while true do
    	local Gamestart = workspace.Gamestart
    	local Intermission = workspace.Intermission
    	local Inttime = workspace.Inttime
    	local Resources = workspace.Resources
	Intermission.Value = true
    	wait()
	while Inttime.Value ~= 0 do -- this will keep counting down till the inttime has reached zero
		wait(1) -- waiting 1 second
    		Inttime.Value = Inttime.Value -1 -- subtracting 1 each second
	end
    	if Inttime.Value == 0  then
        	Intermission.Value = false
        	Inttime.Value = 30
        	wait()
        	Gamestart.Value = true 
        	if Resources.Value == 0 then
            		Gamestart.Value = false
            		local plrs = game.Players:GetPlayers()
            		for _, player in pairs(plrs) do
                		if player.Character and player.Character:FindFirstChild("Humanoid") then
                    			player.Character.Humanoid.Health = 0
	                	end
            		end
        	end
	end
end

This should countdown now :D Hope this helped!

4 comments

0

This should work as well, upvoted :P

0

Thanks :P

0

:3 YUMMY

0

Sorry for the messy code btw, I don't know why it did that lol

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