Problem with loop

So it works normally when the script finds that the value is 0, however, once that happens, it just repeatedly calls the function.

(Yes I know the 0 is in quotes, thats by design)

while true do
	local freq955Length = game.ReplicatedStorage.radioStations.station995.timeLength.Value
	
	if stationStorage.station995.timePosition.Value == "0" then
		update955()
		wait(1)
	else
		print("no")
	end
	wait()
end

Do this:

while true do
	local freq955Length = game.ReplicatedStorage.radioStations.station995.timeLength.Value
	
	if stationStorage.station995.timePosition.Value == "0" then
		update955()
		break
	else
		print("no")
	end
	wait()
end

I just tried this. It doesn’t end up calling the function at all, or printing anything when the value isn’t 0.

Is that a string or number value?

It is a string value, that’s why I put the quotes around the 0.

Ah alr just wanted to make sure. Because if it was a number value the “” wouldnt work

Yeah, I honestly don’t know what to do here. Using break literally breaks everything inside of the if-then statement, and if you don’t use a break, the function just gets called continuously when the value is 0.

while true do
	local value = ...
	
	if value <= 0 then
		dothing()
		break
	end
	
	task.wait()
end

Maybe this work?

The function wasn’t called when I tried this

Alright im guessing you want the loop to stop when it detects the value is 0? correct me if im wrong

Anyways. You could use a “return” instead of break. Heres an example:

local Something = 0


while true do
	Something = Something + 1
	
	if Something == 5 then return end
	
	task.wait(1)
end

print the value, and the value type. If it number then you have to compare number, but if it string then you have to compare string.

Its prints the value, and I tried printing something when the value is 0 and that works.

I want when the value is 0, to call the function, but only once. It just keeps calling the function endlessly when the value hits 0.

I’ve also tried using break, but when I do that, nothing happens period.

Sounds like youd need a debounce here. Whenever the value hits 0 turn your debounce to true. And reset it whenever you need it again.

Forgot to add. This will only run the function if your debouce(In my case the called variable) is set to false.

Example:

local Something = 0
local Called = false


local function DoStuff()
	
end


while true do
	Something = Something + 1
	
	if Something == 5 and not Called then
		Called = true
		DoStuff()
	end
	
	task.wait(1)
end

Am I changing the debounce in the right place? I try this and it just continuously calls the function again.

local stationStorage = game.ReplicatedStorage.radioStations
local marketplaceService = game:GetService("MarketplaceService")
local Called = false

local freq955 = {
	9112960766, 9113662071
} 

--[[local freq955 = {
	10790300515, 10790355109, 10790434741, 10790378585
} -- (R&B)
]]

local function update955()
	print("function called")
	local newSound = freq955[math.random(1, #freq955)]
	stationStorage.station995.Sound.SoundId = "rbxassetid://"..newSound
	
	local songName = marketplaceService:GetProductInfo(newSound).Name

	
	stationStorage.station995.songName.Value = tostring(songName)
	stationStorage.station995.soundID.Value = tostring(newSound)
	print("Song ID: "..tostring(newSound))
	Called = false -- changing debounce here ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←
	wait(0.25)
end

--
stationStorage.station995.timeLengthEvent.OnServerEvent:Connect(function(player, length)
	stationStorage.station995.timeLength.Value = tostring(length)
end)


stationStorage.station995.timePositionEvent.OnServerEvent:Connect(function(player, pos)
	stationStorage.station995.timePosition.Value = tonumber(pos)
end)

--


while true do	
	local value = stationStorage.station995.timePosition.Value

	if value == 0 and not Called then
		Called = true
		update955()
	end

	task.wait(0.1)
end

Oh sorry shoot i noticed an error i did. DONT set the debounce back to false. Thatll just run the function again. Sorry my bad. Just remove the and it should only call it one time. Again sorry my bad. An error on my end

I don’t know if I should have said this sooner, but I don’t want this entire process to happen just once.

Once the function is called, I want it to check for 0 again, and when it is 0 it calls the function again, and the process just keeps going.

local connection = nil 

local function DoStuff()

end

connection = TimePosition:GetPropertyChangedSignal("Value"):Connect(function()
    if TimePosition.Value == 0 then
        connection:Disconnect()
        connection = nil
        DoStuff()
    end
end)

Hope this work

local freq955 = game.ReplicatedStorage.radioStations.station995

freq955.timePosition.Changed:Connect(function(NewValue)
    if NewValue == "0" then
        update955()
    else
        print("not 0")
    end
end)

Just a second late lol

It works but how do I make it continuously loop so it does this forever?