Break in custom function not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to break the custom the loop in a custom function
  2. What is the issue? Include screenshots / videos if possible!
    break statement must be in a loop
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    None
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local spawnPoints = workspace.SpawnPoints:GetChildren()

function SpawnFinger(finger,maxRandomness)
	local randomNumber = math.random(0,maxRandomness)
	if randomNumber == maxRandomness then
		local randomSpawn = math.random(1,#spawnPoints)
		local FingerClone = finger:Clone()
		print(randomNumber)
		FingerClone.Parent = workspace.Fingers
		FingerClone.Handle.CFrame = spawnPoints[randomSpawn].PrimaryPart.CFrame + spawnPoints[randomSpawn].PrimaryPart.CFrame.UpVector
		if FingerClone then
			break
		end
	end
end

while true do
	SpawnFinger(game.ReplicatedStorage.Finger,1)
	wait(1)
end

do return that is all i have to say

1 Like

If you want to keep the breaking condition, you can use the following:

local spawnPoints = workspace.SpawnPoints:GetChildren()

function SpawnFinger(finger,maxRandomness)
	local randomNumber = math.random(0,maxRandomness)
	if randomNumber == maxRandomness then
		local randomSpawn = math.random(1,#spawnPoints)
		local FingerClone = finger:Clone()
		print(randomNumber)
		FingerClone.Parent = workspace.Fingers
		FingerClone.Handle.CFrame = spawnPoints[randomSpawn].PrimaryPart.CFrame + spawnPoints[randomSpawn].PrimaryPart.CFrame.UpVector
		if FingerClone then
			return FingerClone
		end
	end
end

while wait(1) do
	local FingerClone = SpawnFinger(game.ReplicatedStorage.Finger,1)
	if FingerClone then
		break
	end
end

Bare in mind if the function at some point ever has a 2nd return statement added you’ll need to slightly change the conditional check inside the while loop.