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