While loop doesn't stop when number is reached

  1. What do you want to achieve?
    Amount of energy to increase when input is down

  2. What is the issue?
    The While loop that increases the amount of energy doesn’t stop after it reaches the value of the player’s maximum energy

  3. What solutions have you tried so far?
    This is the server script:

local CurrentAnimations = {} 
local cancharge = true

local function startcharge(player,maxki,ki)
	local Character = player.Character
	local fx1 = game.ReplicatedStorage.Particles.Charge:Clone()
	local fx2 = game.ReplicatedStorage.Particles.Spread:Clone()
	fx1.Parent = Character.HumanoidRootPart
	fx2.Parent = Character.HumanoidRootPart
	cancharge = true
	local animc = script.charge:Clone()
	Character.HumanoidRootPart.Anchored = true
	animc.Parent = Character.Humanoid
	CurrentAnimations[player.Name] = Character.Humanoid:LoadAnimation(animc)
	CurrentAnimations[player.Name]:Play()
	while cancharge == true do
		ki.Value = ki.Value + 1	
		if ki.Value == maxki.Value then
			cancharge = false
			print("cap reached")
		end
		wait(0.09)		
	end
end

local function endcharge(player)
	local Character = player.Character
	cancharge = false
	Character.HumanoidRootPart.Charge:Destroy()
	Character.HumanoidRootPart.Spread:Destroy()
	Character.HumanoidRootPart.Anchored = false
	CurrentAnimations[player.Name]:Stop() 
	CurrentAnimations[player.Name] = nil 
end


game.ReplicatedStorage.Events.EnergyCharge.OnServerEvent:Connect(function(player,maxki,ki)
	startcharge(player,maxki,ki)
end)
game.ReplicatedStorage.Events.StopEnergyCharge.OnServerEvent:Connect(function(player,ki)
	endcharge(player)
end)

this script is used with InputBegan and InputEnded

I think you have to break the loop:

local CurrentAnimations = {} 
local cancharge = true

local function startcharge(player,maxki,ki)
	local Character = player.Character
	local fx1 = game.ReplicatedStorage.Particles.Charge:Clone()
	local fx2 = game.ReplicatedStorage.Particles.Spread:Clone()
	fx1.Parent = Character.HumanoidRootPart
	fx2.Parent = Character.HumanoidRootPart
	cancharge = true
	local animc = script.charge:Clone()
	Character.HumanoidRootPart.Anchored = true
	animc.Parent = Character.Humanoid
	CurrentAnimations[player.Name] = Character.Humanoid:LoadAnimation(animc)
	CurrentAnimations[player.Name]:Play()
	while cancharge == true do
		ki.Value = ki.Value + 1	
		if ki.Value == maxki.Value then
			cancharge = false
			print("cap reached")
            break
		end
		wait(0.09)		
	end
end

local function endcharge(player)
	local Character = player.Character
	cancharge = false
	Character.HumanoidRootPart.Charge:Destroy()
	Character.HumanoidRootPart.Spread:Destroy()
	Character.HumanoidRootPart.Anchored = false
	CurrentAnimations[player.Name]:Stop() 
	CurrentAnimations[player.Name] = nil 
end


game.ReplicatedStorage.Events.EnergyCharge.OnServerEvent:Connect(function(player,maxki,ki)
	startcharge(player,maxki,ki)
end)
game.ReplicatedStorage.Events.StopEnergyCharge.OnServerEvent:Connect(function(player,ki)
	endcharge(player)
end)

It didn’t break, i think the problem is the part where it checks if the ki value is the same as the maxki value

if ki.Value == maxki.Value then
cancharge = false
print("cap reached")

because it isn’t printing anything

Have you considered adding an until statement?
It will break the loop once it reaches a desired value / statement / state

1 Like

What’s the value of maxki.Value, is it an integer?
What does these print?

	while cancharge do
		ki.Value += 1	
		print(ki.Value, maxki.Value)
		if ki.Value == maxki.Value then
			cancharge = false
			print("cap reached")
		end
		wait(0.09)		
	end

As you said it isn’t printing anything, it shows that ki.Value == maxki.Value never evalulated to true?
Alternatively you can try ki.Value >= maxki.Value instead.

thanks for this, I didn’t know that the maximum ki was at zero for some reason if it wasn’t for printing

print(ki.Value, maxki.Value)