How do I make this script stop working once magic is full?

  1. What do you want to achieve? a script that regenerates magic until the magic.Value == magicCapacity.Value

  2. What is the issue? the script doesn’t stop once the magic.Value == magicCapacity.Value

  3. What solutions have you tried so far? none yet, which is why I’m here

here’s my script:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Magic = character.Magic
		local MagicCap = character.MagicCapacity
		local magicFull = Magic == MagicCap

		if not magicFull then
			repeat
				wait(0.2)
				Magic.Value += 1
			until magicFull
		end
	end)
end)

all help is appreciated <3

It’s because you are setting magicFull only when the character spawns. You need to change it whenever the Magic’s Value changes, so something like this should work:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Magic = character.Magic
		local MagicCap = character.MagicCapacity
		local magicFull = Magic == MagicCap

		if not magicFull then
			repeat
				wait(0.2)
				Magic.Value += 1
                magicFull = Magic == MagicCap
			until magicFull
		end
	end)
end)

As @aninfiniteinferno said, the magicFull variable is only updated once. However, I think you may not need it at all, if you use a while loop! For example, you can do this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Magic = character.Magic
		local MagicCap = character.MagicCapacity

		while not (Magic == MagicCap) do
			task.wait(0.2) -- task.wait() is the same, but more consistent 
			Magic.Value += 1
		end
	end)
end)

However, this will only ever run once. This is because if the condition is not met, it simply terminates the CharacterAdded thread altogether. However, assuming your Magic and MagicCap values are IntValues or NumberValues, you can do this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Magic = character.Magic
		local MagicCap = character.MagicCapacity
		local Filling = false

		Magic.Changed:Connect(function(value)
			if value < MagicCap.Value then
				Filling = true -- This is so it doesn't create another thread, which would fill it faster
				while Magic.Value < MagicCap.Value do
					Magic.Value += 1
					task.wait(0.2)
				end
				Filling = false
			end
		end)
	end)
end)

This will make it start refilling every time, and ensure it refills at a steady rate.

2 Likes

I’ve tried this but it doesn’t regenerate the magic (an IntValue) I’ll mess around with this and let you know how it goes

I’ve came up with this script but it only work until the magic = the capacity, then it stops working even if the magic goes down. Do you know why this might be happening? I can send you a video if you want (it has a gui that shows the magic if that helps?)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Magic = character.Magic
		local MagicCap = character.MagicCapacity
		local Filling = false
		
		if Magic.Value < MagicCap.Value then
			Filling = true
			while Magic.Value < MagicCap.Value do
				Magic.Value += 1
				task.wait(0.1)
			end
			Filling = false
		end
	end)
end)

This is because it only checks if the magic is below the cap once. Afterwards, it doesn’t have anything else to run. This is why I used :Connect() on the Magic IntValue, since it runs every time the value changes.

1 Like

Another quick question, should this be done in a local script or a server script? and where should I place this?

(right now its a server script in ServerScriptService)

A server script. Otherwise, the client could just change the values/it wouldn’t replicate

1 Like

alright, it’s still not working. do you have any other ideas that I can try to make the magic regenerate? sorry if im asking for too much

Make sure to connect it to the IntValue’s .Changed event.

Haven’t tested it but try this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Magic = character.Magic
		local MagicCap = character.MagicCapacity
		local magicFull = nil

		if not magicFull then
			repeat
				wait(0.2)
				Magic.Value += 1
				if Magic.Value == MagicCap then
					magicFull = true
				end
			until magicFull
		end
	end)
end)

The magic value is never set back to the original value and stays above the magic cap.