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

My while loop continues to loop after the BoolValue is set to false and i dont know what to do?

The Client Script

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

local Mouse = player:GetMouse()

local character = player.Character or player.CharacterAdded:Wait()

local Key = script.Keybind.Value

local debounce = true

UserInputService.InputEnded:Connect(function(Input)
	if (Input.KeyCode == Enum.KeyCode[Key]) then
		game.ReplicatedStorage.ChargeEvent:FireServer()
	end
end)

UserInputService.InputBegan:Connect(function(Input ,IsTyping)
	if IsTyping then return end
	local KeyPressed = Input.KeyCode
	if Input.UserInputType == Enum.UserInputType.Keyboard then
		if KeyPressed == Enum.KeyCode[Key]and debounce and character then
			game.ReplicatedStorage.FireBallEvent:FireServer(Mouse.Hit)
		end
	end
end)


ServerScript with the while loop

game.ReplicatedStorage.FireBallEvent.OnServerEvent:Connect(function(plr, mouse)
	local chargeValue = plr.PlayerGui.BoolValue.Value
	chargeValue = true
	while wait(0.9) and chargeValue == true do
		print("fireball is being charged!")
	end
end)

The ServerScript that controls the BoolValue

game.ReplicatedStorage.ChargeEvent.OnServerEvent:Connect(function(plr)
	local chargeValue = plr.PlayerGui.BoolValue.Value
	chargeValue = false
	print("finished charging!")
end)

This is what the output shows https://gyazo.com/37294c5ff95e9ea36acae0518ff8a75a

1 answer

Well using a "break" function will solve this. "break" function breaks a while loop.

game.ReplicatedStorage.FireBallEvent.OnServerEvent:Connect(function(plr, mouse)
	local chargeValue = plr.PlayerGui.BoolValue.Value
	chargeValue = true
	while wait(0.9) and chargeValue == true do
		print("fireball is being charged!")
		if chargeValue == false then -- This detects if the charge value is set to false and if it is,
			break -- then the while loop will be broken and it will stop printing.
		end
	end
end)

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