Stopping While Loops

I’m making a jojo game and for the barrage I’m instancing extra arms in a while loop. The client fires to the server when I hold down on a key and starts the barrage. When I release the key the client fires again stopping the barrage. However when releasing the key the while loop doesn’t stop instead it creates another while loop and stops that one. I know it’s because I’m firing the server twice when pressing and releasing the key but I’m not sure how to solve it.

Show us the script for further context on your issue

local script
local active = false

UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local stand = Player.Character:FindFirstChild(“Stand”)
if input.KeyCode == Enum.KeyCode.E and stand and active == false then
active = true
RE:FireServer(active)
end
end

end)

UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local stand = Player.Character:FindFirstChild(“Stand”)
if input.KeyCode == Enum.KeyCode.E and stand and active == true then
active = false
RE:FireServer(active)
end
end

Server Script
RE.OnServerEvent:Connect(function(player, active)

local stand = player.Character:FindFirstChild("Stand")
local standHRP = stand:WaitForChild("HumanoidRootPart")
local weld = standHRP:FindFirstChild("StandWeld")
local function Barrage(stand,LeftRight)
	if LeftRight == 1 then
		local LeftArm = script:FindFirstChild("LeftCDArm"):Clone()
		LeftArm.PrimaryPart = LeftArm.HumanoidRootPart
		LeftArm:SetPrimaryPartCFrame(stand.HumanoidRootPart.CFrame * CFrame.Angles(0,0,math.rad(math.random(-45,45))))
		LeftArm.Parent = stand

		local weld = Instance.new("ManualWeld")
		weld.Name = "BarrageWeld"
		weld.Part0 = LeftArm.PrimaryPart
		weld.Part1 = stand.HumanoidRootPart
		weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
		weld.Parent = weld.Part0

		local weld = Instance.new("ManualWeld")
		weld.Name = "BarrageWeld"
		weld.Part0 = LeftArm.UpperTorso
		weld.Part1 = stand.HumanoidRootPart
		weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
		weld.Parent = weld.Part0

		local BarrageAnim = script:WaitForChild("BarrageAnim")
		local LoadAnim = LeftArm.AnimControl:LoadAnimation(BarrageAnim)
		LoadAnim:Play(0,10,7)
		Debris:AddItem(LeftArm,.6)

		wait(.1)
		for _, part in pairs(LeftArm:GetChildren()) do
			if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then
				local goal = {}
				goal.Transparency = 1
				local info = TweenInfo.new(.3)
				local tween = TweenService:Create(part,info,goal)
				tween:Play()
			end
		end

	elseif LeftRight == 2 then

		local RightArm = script:FindFirstChild("RightCDArm"):Clone()
		RightArm.PrimaryPart = RightArm.HumanoidRootPart
		RightArm:SetPrimaryPartCFrame(stand.HumanoidRootPart.CFrame * CFrame.Angles(0,0,math.rad(math.random(-45,30))))
		RightArm.Parent = stand

		local weld = Instance.new("ManualWeld")
		weld.Name = "BarrageWeld"
		weld.Part0 = RightArm.PrimaryPart
		weld.Part1 = stand.HumanoidRootPart
		weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
		weld.Parent = weld.Part0

		local weld = Instance.new("ManualWeld")
		weld.Name = "BarrageWeld"
		weld.Part0 = RightArm.UpperTorso
		weld.Part1 = stand.HumanoidRootPart
		weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
		weld.Parent = weld.Part0

		local BarrageAnim = script:WaitForChild("BarrageAnim")
		local LoadAnim = RightArm.AnimControl:LoadAnimation(BarrageAnim)
		LoadAnim:Play(0,10,7)
		Debris:AddItem(RightArm,.6)

		wait(.1)
		for _, part in pairs(RightArm:GetChildren()) do
			if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then
				local goal = {}
				goal.Transparency = 1
				local info = TweenInfo.new(.3)
				local tween = TweenService:Create(part,info,goal)
				tween:Play()
			end
		end

	end
end
local Barraging = false
local Stop = coroutine.create(function()
	Barraging = false
end)

if active == true then
	-- Tweens Stand in front of character
	local goal = {}
	goal.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
	goal.C1 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame * CFrame.new(0,1,-2))
	local info = TweenInfo.new(.25)
	local tween = TweenService:Create(weld,info,goal)
	tween:Play()
	Barraging = true

end

if active == false then
	coroutine.resume(Stop)
end

while Barraging do
	wait()
	Barrage(stand,2)
	Barrage(stand,1)
end

end)

add

if not barraging then break end

to it

Instead of having two if statements for active’s bool, use else. Also, if active is false, then you need to set Barraging to false as well

if active then
	-- Tweens Stand in front of character
	local goal = {}
	goal.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
	goal.C1 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame * CFrame.new(0,1,-2))
	local info = TweenInfo.new(.25)
	local tween = TweenService:Create(weld,info,goal)
	tween:Play()
	Barraging = true
else
    Barraging = false
	coroutine.resume(Stop)
end

Still doesn’t work just keeps running the loop

Ive tried that but because I’m firing the to the server twice it doesn’t use the while loop for the first time I fired

I found a solution. I instanced a bool value and let the while loop run on that. Thanks to the two that tried to help me.

1 Like