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

I'm trying to make a mana charge script but it got a timeout?

Asked by
epicnt 13
4 years ago

I'm trying to make a mana charge script that goes back down to 0 slowly after reaching 100.

It's not complete but I decided to test. Didn't work and told me the game script had a timeout Here's the script:

local RS = game:GetService("ReplicatedStorage")
local RE = RS.ManaToggle

RE.onServerEvent:Connect(function()
    local ManaValue = Instance.new("IntValue", game.Players.LocalPlayer)

    ManaValue.Value = 0

    if ManaValue.Value == 100 then return 
    else 
        while true do 
            ManaValue.Value = ManaValue.Value + 1
        end
        while true do
        wait(5)
        print(ManaValue.Value)
        end
    end   
end)

Please Help.

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Loops can run faster than ROBLOX can handle, you always need to add a wait() within any form of repetitive reiteration

spawn(function()
    while true do wait()
        --// Code
    end
end)
--// Code
0
I'll try it. epicnt 13 — 4y
0
It stopped timing out but it doesn't seem to print or work... epicnt 13 — 4y
0
This is because you’re loop is yielding the thread below; the thread will interrupt the Script until it’s finished, meaning your while loop will never run. To solve this, you have to split a new thread using spawn() or coroutine. Ziffixture 6913 — 4y
0
I'll try coroutine but I'm not sure how to use it. epicnt 13 — 4y
0
Oh, I realized you added code, thanks. epicnt 13 — 4y
Ad

Answer this question