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

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

Asked by 5 years ago
Edited 5 years ago

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.

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 BronzedMocha 60 — 5y
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? DogeDark211 81 — 5y
0
what output are you getting, also are you getting any errors? PoePoeCannon 519 — 5y
0
I am getting no output or errors. Just adds 1 like 5 times a second without waiting. DogeDark211 81 — 5y
View all comments (2 more)
0
You need to add a debounce PoePoeCannon 519 — 5y
0
It works! Thank you. DogeDark211 81 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)
0
remember this in the future as it will be extremely helpful! Good luck with your game. :) PoePoeCannon 519 — 5y
0
Thanks! I usually use debounces a lot for tools and I didn't know it would come in handy here. DogeDark211 81 — 5y
Ad

Answer this question