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

If than statement not running inside while loop?

So I've been working on this project, problem is the while loop is acting very strange.

Tool.Equipped:Connect(function()
	print(Player)
	GUI.Parent = Player.PlayerGui
		while FuelLelf == true do
			if CurrentFuel ~= 0 then
	   	 		CurrentFuel = CurrentFuel - FuelDecreaseNum 
				print(CurrentFuel)
				wait()
				if FuelLelf == 0 then
					wait()
					print("This should print")
					LightPart.PointLight.Enabled = false
					LightPart.Transparency = 1
					FuelLelf = false
				end
			end
		print(FuelLelf)
		wait(1)
       end
	end)

(forgive my messy code writing) What's happening is, the code is working as intended mostly, but then it gets down to 0 and just stops, that would be good if the code also disabled the PointLigt, and made the LightPart transparent, but it does not. And stranger still, after 0 is printed the while loop continues to print true, I'm not sure what's happening, all I can think is the second if statement is not running, but I don't know why it would not run? Any help would be appriciated.

1 answer

✓ Accepted answer

This is because the if-statement is embedded in the conditional scope designated to proceed while CurrentFuel is not zero; when the variable reaches zero, FuelLelf == 0 will not be checked.

This is a simple fix: simply use an else block, since the prerequisite is an inversion of the original condition:

if (CurrentFuel ~= 0) then
	---------------
	print(CurrentFuel)
else
	print("Out of fuel!")
end

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