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

Trying to break a Heartbeat help?

Asked by
NotSoNorm 777 Moderation Voter
8 years ago

So I'm trying to break a loop, and I've done it before but since it's a heartbeat service how would I break that?

function mainmod:Spin(timeforspin)
    local timspin = timeforspin
    local keeploop = true
    script.Parent.MaterialPicker.Toolbar.Loader.Visible = true
    RunService.Heartbeat:connect(function(Step) --Needing to break this
        if keeploop then
            script.Parent.MaterialPicker.Toolbar.Loader.Rotation = script.Parent.MaterialPicker.Toolbar.Loader.Rotation + 5
        else
            script.Parent.MaterialPicker.Toolbar.Loader.Rotation = 0 --after this
        end
    end)
    coroutine.wrap(function()
        wait(timspin)
        keeploop = false
        script.Parent.MaterialPicker.Toolbar.Loader.Visible = false
    end)()
end

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

The :connect method returns a RBXScriptSignal.

That signal has a :disconnect() method which stops it from listening. Thus your code would look something like this:

    local loop = RunService.Heartbeat:connect(function(Step)
        if keeploop then
            script.Parent.MaterialPicker.Toolbar.Loader.Rotation = script.Parent.MaterialPicker.Toolbar.Loader.Rotation + 5
        else
            script.Parent.MaterialPicker.Toolbar.Loader.Rotation = 0
            loop:disconnect()
        end
    end)

I have some more recommendations about improving this code.

Don't use coroutine.wrap when spawn is sufficient -- it's so much shorter, cleaner, and shows your intent better:

    spawn(function()
        wait(timspin)
        keeploop = false
        script.Parent.MaterialPicker.Toolbar.Loader.Visible = false
    end)

even better, use delay instead of spawning something that waits:

    delay(timespin, function()
        keeploop = false
        script.Parent.MaterialPicker.Toolbar.Loader.Visible = false
    end)

instead of setting keeploop, you might as well just :disconnect the connection. You can also eliminate timeforspin since just the parameter works fine:

function mainmod:Spin(timeforspin)
    script.Parent.MaterialPicker.Toolbar.Loader.Visible = true
    local loop = RunService.Heartbeat:connect(function(Step)
        script.Parent.MaterialPicker.Toolbar.Loader.Rotation = script.Parent.MaterialPicker.Toolbar.Loader.Rotation + 5
    end)
    delay(timeforspin, function()
        loop:disconnect()
        script.Parent.MaterialPicker.Toolbar.Loader.Rotation = 0
        script.Parent.MaterialPicker.Toolbar.Loader.Visible = false
    end)
end

Since you say it 5 times, you should just save script.Parent.MaterialPicker.ToolBar.Loader to a variable:

local gui = script.Parent.MaterialPicker.ToolBar.Loader

function mainmod:Spin(timeforspin)
    gui.Visible = true
    local loop = RunService.Heartbeat:connect(function(Step)
        gui.Rotation = gui.Rotation + 5
    end)
    delay(timeforspin, function()
        loop:disconnect()
        gui.Rotation = 0
        gui.Visible = false
    end)
end

0
Thanks dude, You're great! NotSoNorm 777 — 8y
Ad

Answer this question