Game shuts down, and studio crashes when value is changed?

Everything has been going fine with development lately. Until I’ve made a television script where a value is changed to turn on the TV broadcast. However, when the value is changed, in Studio, it freezes and crashes, while ingame, it just shuts down the entire server.

while true do
BroadcastVal:GetPropertyChangedSignal("Value"):Connect(function()
if BroadcastVal.Value == true then 
	BeeperScript.Enabled = false
	TVAudio.Playing = true

This is the script for the value changing function.

local BroadcastValue = script.Parent

game:GetService("Lighting").LightingChanged:Connect(function()
	if game.Lighting.ClockTime >= 9 then
		BroadcastValue.Value = true
	else
	end
end)

This is the script for making the value turn true at 9 AM.

1 Like

You have a while true do loop at the start, but does it have any returns? While true do loops also have this tendency to run indefinitely, and will 100% crash the game unless you yield it.
Example:

while true do
        -- doing some stuff here...
        if someValue == something then return end
end

In some cases it doesn’t matter which returns you have or what if statements you have. If your loop runs indefinitely without yielding it, you can bet it’ll crash your game due to memory exhaustion.

while true do
        -- do some stuff here
        -- also check the same condition
        task.wait()
end

That won’t crash your game, even if you have a simple task.wait() without any parameters. Your loop will still run indefinitely until something happens, but at least it’ll run more efficiently (although there are more efficient ways…)

Hope that helps!

1 Like

As mentioned by Siniston, the issue likely is in the while loop.

I would recommend either:

  • Get rid of the while true do bit - the GetPropertyChangedSignal should be sufficient
  • Keep the while true do bit, but Replace :Connect(function...) with :Wait() (this will mean the loop will only iterate once per property change
1 Like

Hi!

The script is

while true do
BroadcastVal.Changed:Connect(function()
if BroadcastVal.Value == true then 
-- stuff

elseif BroadcastVal.Value == false then return end
end)
end

Where should I put the task.wait()?
Apologies, I’m not a scripter haha

You have to have the task.wait() inside of the while true do loop.

while true do
        BroadcastVal.Changed:Connect(function()
        if BroadcastVal.Value == true then 
                -- stuff

        elseif BroadcastVal.Value == false then return end
        --end) --> not needed.
        task.wait() --> here. You can honestly add it anywhere, doesn't matter... as long as it's outside of any if/elseif condition...
end

Try and see if that works. Also added some proper indentation to your code to make the logic easier to track.

EDIT: Also make sure to cleanup your connection inside of the loop:

local connection
connection = BroadcastVal.Changed:Connect(function()
        -- do something
end)
-- Better to establish the connection outside of the while true do loop to avoid
-- creating extra copies of the same connection, causing you a massive memory leak.

while true do
        if BroadcastVal.Value == true then 
                -- stuff
                connection:Disconnect() --> Disconnect your connection (if this is where it should disconnect.)
        elseif BroadcastVal.Value == false then return end
        task.wait() --> here. You can honestly add it anywhere, doesn't matter... as long as it's outside of any if/elseif condition...
end

Props to @SeargentAUS for pointing out the major flaw.
Also as @SeargentAUS pointed out… it’s probably better to just not have a while true do loop in this case.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.