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

Cannot detect if a number value hits 0?

The countdown works and the number value in replicated storage is 0 but the teleportation does not work and it won't print "zero"

local function gi()
local int = game:GetService("ReplicatedStorage").int

local bo = game:GetService("ReplicatedStorage").gfd

while true do
    wait(1)
int.Changed:Connect(function()
script.Parent.Text = "Intermission = "..int.Value
end)
if bo.Value == true then
    int.Value = int.Value - 10
    if int.Value == 0 then
        bo.Value = false
end
    else
    int.Value = 0
    end 
end



if int.Value == 0 then
	print("zero")
  script.Parent.Visible = false
   for _, player in pairs(game.Players:GetPlayers()) do
       if player.Character  then
        player.Character:SetPrimaryPartCFrame(CFrame.new(-184, 0.5, 141))   
            end
		end
	end
end



game:GetService("ReplicatedStorage").fds.OnClientEvent:Connect(gi)

2 answers

You've got an endless while loop there. If you want to run both in the same script, you'll have to wrap the loop in a coroutine. Let me show you how it works:

coroutine.resume(coroutine.create(function()
	while true do
		wait(1)
		int.Changed:Connect(function()
			script.Parent.Text = "Intermission = "..int.Value
		end)
		if bo.Value == true then
	    		int.Value = int.Value - 10
	    		if int.Value == 0 then
	        		bo.Value = false
			end
		else
	    		int.Value = 0
	    	end
	end
end))

Basically what is happening here is that I created a coroutine (which is a routine running alongside your program) and put your while loop in there. By doing this I allow the script to move past this point because it is no longer stuck in that loop now.

EDIT:

Also, as was mentioned, you may also want to regularely check whether the countdown has reached 0. If I were you, I would just put that condition inside the loop and break the loop once the condition is true.

1 comment

0

Instead of coroutine.resume(coroutine.create(function() end)), do coroutine.wrap(function() end)

Hey, Dude, I know what your trying to do here. But haven't you ever searched it up yet? Well Here is some scripts I made for you. Put these in your ``IntValue.

--Put In IntValue
while true do
	if script.Parent.Value == 0 then
		--Your code here
	end
	wait()
end 

Then do it again but with script:

--Put In IntValue
while true do 
	Script.Parent.Value = script.Parent.Value-1 --the number of seconds you want to countdown.
	wait(1) --The wait between countdowns
end

If ya have any problems, I'll help ya.

.

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