Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

5 comments

0

I'll try it.

0

It stopped timing out but it doesn't seem to print or work...

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.

0

I'll try coroutine but I'm not sure how to use it.

0

Oh, I realized you added code, thanks.

This discussion is preserved as it was. New answers, comments, and votes are closed.