"For" Loops Completly Freeze my Game Both in Studio and In-Game?

So for some unknown reason this script is causing my game to freeze and crash:


function Intro()
    
    print("hi?")

    script.Parent.Menu.Visible = true

    for Transparency = 1, 0, -.1 do
        script.Parent.Menu.Intro.Text.TextTransparency = Transparency
        script.Parent.Menu.Intro.Image.ImageTransparency = Transparency
        wait()
    end

    SoS.intro:Play()

    wait(1.5)

    for Transparency = 0, 1, .1 do
        script.Parent.Menu.Intro.Text.TextTransparency = Transparency
        script.Parent.Menu.Intro.Image.ImageTransparency = Transparency
        wait()
    end

    for Transparency = 1, 0, -.1 do
        script.Parent.Menu.Intro.Text1.TextTransparency = Transparency
        script.Parent.Menu.Intro.Skip.BackgroundTransparency = Transparency
        wait(.01)
    end

    for Transparency = script.Parent.Menu.Intro.Text2.TextTransparency, 0, -.1 do
        script.Parent.Menu.Intro.Text2.TextTransparency = Transparency
        wait()
    end
    
    wait(1.5)

    for Transparency = script.Parent.Menu.Intro.Text3.TextTransparency, 0, -.1 do
        script.Parent.Menu.Intro.Text3.TextTransparency = Transparency
        wait()
    end

    wait(2)

    for Transparency = script.Parent.Menu.Intro.BackgroundTransparency, 0, -.1 do
        script.Parent.Menu.Intro.BackgroundTransparency = Transparency
        wait()
    end

    script.Parent.Menu.Intro.Visible = false

    MenuFunction()

end

I think that this part is when it freezes…


    for Transparency = 1, 0, -.1 do
        script.Parent.Menu.Intro.Text.TextTransparency = Transparency
        script.Parent.Menu.Intro.Image.ImageTransparency = Transparency
        wait()
    end

    SoS.intro:Play()

    wait(1.5)

    for Transparency = 0, 1, .1 do
        script.Parent.Menu.Intro.Text.TextTransparency = Transparency
        script.Parent.Menu.Intro.Image.ImageTransparency = Transparency
        wait()
    end

I have re-written this code 2 times and it keeps happening.

I’m 99% sure that its that function thats causing the freeze, because when it’s not fired everything works fine.

Game: https://www.roblox.com/games/2668658373/Iron-Curtain-Development

Does it still freeze when you run a command in the command bar.

Ect: to make the intro transparent, does it freeze

1 Like

I checked without the sound being played, and it still freezes; so its the loops.

Run this code in the command bar:

game:GetService("ScriptContext"):SetTimeout(1)

then run your for loops script.

It should prevent your studio from freezing, and additionally should output the Game script timeout error in the output when a loop tries to run without yielding for more than 1 second.
From that point just check the line it errored at.

4 Likes

It could be that you’re directly referencing these objects, instead of checking first that they exist.

Use WaitForChild on gui objects to make sure they exist.
E. G.

local gui = script.Parent
local menu = gui:WaitForChild("Mene") 
local intro = menu:WaitForChild("Intro") 

--etc... One you have these references you'll find it easier to also program these loops. 
--you can improve this script by looking into the Tween Service aswell. 

This won’t be an issue unless these instances are outside of ReplicatedFirst, and the code’s container is a LocalScript inside ReplicatedFirst, and the code is running before DataModel.Loaded fires.

We know this isn’t the case because he’s crashing, not receiving an error.

1 Like

Could we see the contents of MenuFunction?

So I joined your game and after getting crashed I checked my logs in %localappdata%\Roblox\logs

This is the output:

Error: Game script timeout
    Stack Begin
    Script 'Players.Amiaa16.PlayerGui.ResetOnSpawn.GuiScript', Line 733
    Stack End

This is where your infinite loop is at ^.^

4 Likes