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

While loop wait() not waiting, no errors, spams +1?

Hello, I'm making a terminal and it has a while loop that adds +1 to a intValue. Whenever it runs it just spams +1.

I have filtering enabled, I am using a server script.

--tIme is defined up more in the script
while tIme.Value < 10 do --runs when tIme.Value is lower than ten, low number for testing
	wait(1) --Wait nevers waits
	tIme.Value = tIme.Value + 1 -- adds 1 to the value
end

Any help would be appreciated.

EDIT: Whole script.

local gameRunning = game:GetService("ServerScriptService").Start.running
local beam = script.Parent.Parent.Beam
local text = game:GetService("ServerScriptService").Start.text
local timer = game:GetService("ServerScriptService").Start.timer
local tIme = script.Parent.time


script.Parent.Touched:connect(function(hit)
	if gameRunning.Value == false then return end
		if hit.Parent:FindFirstChild("Humanoid") then
		local char = hit.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
	
		if plr.TeamColor == BrickColor.new("Really red") then
			beam.BrickColor = BrickColor.new("Really red")
			text.Value = "Red team owns the hill!"
			--Here to
			while tIme.Value < 10 do
				wait(1)
				tIme.Value = tIme.Value + 1
			end
			--Here 
			text.Value = "Red has won!"
		elseif plr.TeamColor == BrickColor.new("Really blue") then
			beam.BrickColor = BrickColor.new("Really blue")
			text.Value = "Blue team owns the hill!"
			--Here
			while tIme.Value < 10 do
				wait(1)
				tIme.Value = tIme.Value + 1
			end
			--Here
			text.Value = "Blue has won!"
		end
	end
end)

Between the --here are where the while loops I am having problems with are.

6 comments

0

I copied that and had that going on my side, it works fine for me. The problem exists outside of the script you're showing

0

I have a function that checks if the intValue changes and it updates a StringValue.Value to the IntValue.Value. Would this be the cause?

0

what output are you getting, also are you getting any errors?

0

I am getting no output or errors. Just adds 1 like 5 times a second without waiting.

0

You need to add a debounce

0

It works! Thank you.

1 answer

✓ Accepted answer

You should try adding a debounce like this

local gameRunning = game:GetService("ServerScriptService").Start.running
local beam = script.Parent.Parent.Beam
local text = game:GetService("ServerScriptService").Start.text
local timer = game:GetService("ServerScriptService").Start.timer
local tIme = script.Parent.time

local debounce = false

script.Parent.Touched:connect(function(hit)
	if debounce == false then -- check debounce value
	    debounce = true -- change debounce so it wont run again
	    if gameRunning.Value == false then return end
	        if hit.Parent:FindFirstChild("Humanoid") then
	        local char = hit.Parent
	        local plr = game.Players:GetPlayerFromCharacter(char)
	
	        if plr.TeamColor == BrickColor.new("Really red") then
	            beam.BrickColor = BrickColor.new("Really red")
	            text.Value = "Red team owns the hill!"
	            --Here to
	            while tIme.Value < 10 do
	                wait(1)
	                tIme.Value = tIme.Value + 1
	            end
	            --Here 
	            text.Value = "Red has won!"
	        elseif plr.TeamColor == BrickColor.new("Really blue") then
	            beam.BrickColor = BrickColor.new("Really blue")
	            text.Value = "Blue team owns the hill!"
	            --Here
	            while tIme.Value < 10 do
	                wait(1)
	                tIme.Value = tIme.Value + 1
	            end
	            --Here
	            text.Value = "Blue has won!"
	        end
	    end
	    debounce = false -- set it back once everything is done
	end
end)

2 comments

0

remember this in the future as it will be extremely helpful! Good luck with your game. :)

0

Thanks! I usually use debounces a lot for tools and I didn't know it would come in handy here.

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