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

Why Does This Script Not Recognize a Value Change?

This is a script that is supposed to regenerate Magic over time for the player. The value for Magic has already been established for the player in the leaderstats. **This script executes fine, but there is a problem: When the player levels up, the Magic continues to regenerate at the same rate rather than increasing at a rate to accommodate the new level of the player. ** The script does not recognize that the level has changed, therefore, it continues to regenerate at the same rate, unless the player resets. Why is this?

--This script is in the StarterGui
player = script.Parent.Parent
leaderstats = player.leaderstats
level = script.Parent.Parent.leaderstats.Level.Value --When this value changes, the script does not recognize it, so the Magic continues to regenerate at the same rate. Why?
magic = leaderstats.Stats.Magic
maxmagic = (20+level*2) --The problem with recognizing the level also causes a problem here; when the player levels up, this value increases as well, because the value of the new level factors into this equation. But since the script doesn't recognize the level, this doesn't change either. Now, when the Magic regenerates, it stops here, short of the new max.
speed = leaderstats.Stats.MagicSpeed.Value*.05
rate = math.ceil(maxmagic*.045)

while true do
	wait()
	if magic.Value < maxmagic then
		wait(speed)
		magic.Value = magic.Value + rate
	elseif magic.Value > maxmagic then
		magic.Value = maxmagic
	end
end

1 answer

✓ Accepted answer

The error i that i think is in here is this: When the rate is first set lets say the rate equals to 20, it will stay that way since it is only being set once, but when put in the while loop, it will reset the rate value

--This script is in the StarterGui
player = script.Parent.Parent
leaderstats = player.leaderstats
level = leaderstats:WaitForChild('level') -- Easier
magic = leaderstats.Stats.Magic
maxmagic = (20+level.Value*2) 
speed = leaderstats.Stats.MagicSpeed.Value*.05
rate = maxmagic*.045
 
while true do
local maxmagic = (20+level.Value*2) 
local rate =  maxmagic*.045
    wait()
    if magic.Value < maxmagic then
        wait(speed)
        magic.Value = magic.Value + rate
    elseif magic.Value > maxmagic then
        magic.Value = maxmagic
    end
end

2 comments

0

Thank you very much, this worked! Now I get it!

0

Np :D

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