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

How to make magic regen script work using roblox's health script?

I tried using roblox’s health script to create a magic regen script using value’s but it seems to not work. Why is that?

local MAGICREGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local MAGICREGEN_STEP = 1


--------------------------------------------------------------------------------
local Character = script.Parent
local magic = Character:WaitForChild'Magic'
--------------------------------------------------------------------------------


while true do
	while magic.Value < magic.MaxMagic.Value do
		local dt = wait(MAGICREGEN_STEP)
		local dh = dt*MAGICREGEN_RATE*magic.MaxMagic.Value
		magic.Value = math.min(magic.Value + dh, magic.MaxMagic.Value)
	end
	magic.Value.Changed:Wait()
end

2 comments

0

Can I ask what 'magic' is? Is it a NumberValue?

0

obviously yea

1 answer

First of all, you could do:

while true do
    if magic.Value < magic.MaxMagic.Value then
		
    end
end

instead of:

while true do
    while magic.Value < magic.MaxMagic.Value do
		
    end
end

I don't know the problem but this works:

local MAGICREGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local MAGICREGEN_STEP = 1


--------------------------------------------------------------------------------
local Character = script.Parent
local magic = Character:WaitForChild("Magic")
--------------------------------------------------------------------------------


while true do
	if magic.Value < magic.MaxMagic.Value then
		wait(0.01)
		local dh = MAGICREGEN_STEP * MAGICREGEN_RATE * magic.MaxMagic.Value
		magic.Value = math.min(magic.Value + dh, magic.MaxMagic.Value)
	end
end

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