Why does the parts keep spawning

So Im making my game I made a simple part spawn script but when it returns “false” the loop never breaks I dont know why but can someone help me?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.SpawnPartEvent

RemoteEvent.OnServerEvent:Connect(function(player, stat)
	local character = player.Character
	print("Received stat:", stat)

	if not character then
		return
	end
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if not humanoidRootPart then
		return
	end
	while true do
		if stat == "false" then
			break
			print("a")
		end
		local part = Instance.new("Part",workspace)
		part.Size = Vector3.new(15, .1, 15)
		part.Position = character["Right Leg"].Position
		part.Anchored = true

		task.spawn(function()
			task.wait(3)
			part:Destroy()
		end)
		wait(0.1)
	end
end)

It prints false, and also “a” but the parts keep spawning, here is the client code: (if needed)

script.Parent.Equipped:Connect(function()
	game:GetService("ReplicatedStorage").SpawnPartEvent:FireServer("true")
end)
script.Parent.Unequipped:Connect(function()
	game:GetService("ReplicatedStorage").SpawnPartEvent:FireServer("false")
	print("aaaaa")
end)

(prints “aaaaa” also)
Can someone help me?

That’s a string, not a boolean. Should be just false.

ik, but i pass it as a string game:GetService("ReplicatedStorage").SpawnPartEvent:FireServer("false")
if stat == "false" then

Also as I mentioned before I said that it pases the if statement BUT the while loop dosent break

have you figured out a solution yet?

I’m not a genius when it comes to break, but have you tried to see if it was just breaking out of the if statement? But as I mentioned before I don’t use break all that often so I could definitely be wrong.

local script:

script.Parent.Equipped:Connect(function()
	game:GetService("ReplicatedStorage").SpawnPartEvent:FireServer(true)
end)
script.Parent.Unequipped:Connect(function()
	game:GetService("ReplicatedStorage").SpawnPartEvent:FireServer(false)
end)

server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.SpawnPartEvent
local looping = false

RemoteEvent.OnServerEvent:Connect(function(player, stat)
	local character = player.Character or player.CharacterAdded:Wait()
	print("Received stat:", stat)
	
	if stat == false then
		
		looping = false
		
	elseif stat == true then
		
		looping = true
		
	end

	while looping == true do
		
		local part = Instance.new("Part",workspace)
		part.Size = Vector3.new(15, .1, 15)
		part.Position = character["Right Leg"].Position
		part.Anchored = true
		
		game.Debris:AddItem(part, 3)
		
		wait(0.1)
	end
end)

Thank you so much for the help!!!

1 Like

Sorry for not replying I was at school

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.