Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Dec_ade 47
4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)
Ad

Answer this question