While loop aren't stopping even if the condition is false

Hello i am trying to make a system that charge the fuel when the character isn’t flying or in the air. My issue is that the while loop are not stopping even when the humanoidstatetype is freefall
Hope you can help me check the code down bellow. also sorry for some wrong spellings of word “Fuel”

```char:WaitForChild("Humanoid").StateChanged:Connect(function(OldState,NewState)
local FuelLeft, MaxFeul = script.Parent.Parent.Events:WaitForChild("FuelAmount"):InvokeServer()
while FuelLeft < MaxFeul and NewState ~= Enum.HumanoidStateType.Freefall and NewState ~= Enum.HumanoidStateType.Jumping do
	AddFuelEvent:FireServer(MaxFeul * 2/100)
	print(JetFeul.Value)
	wait(0.5)
end

end)```

i tried another way with if statement, but still same

	local FuelLeft, MaxFeul = script.Parent.Parent.Events:WaitForChild("FuelAmount"):InvokeServer()
	while FuelLeft < MaxFeul do
		if NewState ~= Enum.HumanoidStateType.Freefall and NewState ~= Enum.HumanoidStateType.Jumping then
			AddFuelEvent:FireServer(MaxFeul * 2/100)
			print(JetFeul.Value)
		end
		wait(0.5)
	end
end)```
1 Like

I just did this and it printed it fine.

local player = game.Players.LocalPlayer
local humanoid = script.Parent.Humanoid
while wait(1) do
print(humanoid:GetState() == Enum.HumanoidStateType.Freefall)
print(humanoid:GetState() == Enum.HumanoidStateType.Jumping)
end

Output:

Have you tried just using humanoid:GetState() instead of saving it into the variable NewState?

2 Likes

Thx for the idea. i changed the code how you told me to do and it worked, but i was wondering does that method cause lag.

while wait(1) do
	local FuelLeft, MaxFuel = script.Parent.Parent.Events:WaitForChild("FuelAmount"):InvokeServer()
	if FuelLeft < MaxFuel then
		if char.Humanoid:GetState() == Enum.HumanoidStateType.Freefall or char.Humanoid:GetState() == Enum.HumanoidStateType.Jumping then
			 -- Nothing
		else
			AddFuelEvent:FireServer(MaxFuel * 2/100)
			print(JetFuel.Value)
		end	
	end
end```

it wouldn’t reach the remote bandwidth limit with this implementation but it is pretty redundant and insecure having this be handled by the client and remotes when the server can already view the humanoid’s states & having a loop on the server would have very similar performances to doing it locally without making it an open market to exploiters

1 Like