Function not restarting

Basically, I want this to restart the function if the value is false, but instead it just gives up. I thought “return” would make it re-read the script but no. Help would be appreciated.

Script:

local function randomiseChair(clone)
	local random = math.random(1,4)
	if random == 1 then
		if script.Parent.waitingChair1.taken.Value == false then
			clone.Humanoid:MoveTo(script.Parent.waitingChair1.Seat.Position, script.Parent.waitingChair1.Seat)
			clone.Humanoid.MoveToFinished:Wait()
			clone.Humanoid.Sit = true
			script.Parent.waitingChair1.taken.Value = true
		else
			return
		end
	end

	if random == 2 then
		if script.Parent.waitingChair2.taken.Value == false then
			clone.Humanoid:MoveTo(script.Parent.waitingChair2.Seat.Position, script.Parent.waitingChair2.Seat)
			clone.Humanoid.MoveToFinished:Wait()
			clone.Humanoid.Sit = true
			script.Parent.waitingChair2.taken.Value = true
		else
			return
		end
	end

	if random == 3 then
		if script.Parent.waitingChair3.taken.Value == false then
			clone.Humanoid:MoveTo(script.Parent.waitingChair3.Seat.Position, script.Parent.waitingChair1.Seat)
			clone.Humanoid.MoveToFinished:Wait()
			clone.Humanoid.Sit = true
			script.Parent.waitingChair3.taken.Value = true
		else
			return
		end
	end

	if random == 4 then
		if script.Parent.waitingChair4.taken.Value == false then
			clone.Humanoid:MoveTo(script.Parent.waitingChair4.Seat.Position, script.Parent.waitingChair4.Seat)
			clone.Humanoid.MoveToFinished:Wait()
			clone.Humanoid.Sit = true
			script.Parent.waitingChair4.taken.Value = true
		else
			return
		end
	end
end

You can just call the function again:

local function randomiseChair(clone)
	local random = math.random(1,4)
	if random == 1 then
		if script.Parent.waitingChair1.taken.Value == false then
			clone.Humanoid:MoveTo(script.Parent.waitingChair1.Seat.Position, script.Parent.waitingChair1.Seat)
			clone.Humanoid.MoveToFinished:Wait()
			clone.Humanoid.Sit = true
			script.Parent.waitingChair1.taken.Value = true
		else
			randomiseChair(clone)
		end
	end

	if random == 2 then
		if script.Parent.waitingChair2.taken.Value == false then
			clone.Humanoid:MoveTo(script.Parent.waitingChair2.Seat.Position, script.Parent.waitingChair2.Seat)
			clone.Humanoid.MoveToFinished:Wait()
			clone.Humanoid.Sit = true
			script.Parent.waitingChair2.taken.Value = true
		else
			randomiseChair(clone)
		end
	end

	if random == 3 then
		if script.Parent.waitingChair3.taken.Value == false then
			clone.Humanoid:MoveTo(script.Parent.waitingChair3.Seat.Position, script.Parent.waitingChair1.Seat)
			clone.Humanoid.MoveToFinished:Wait()
			clone.Humanoid.Sit = true
			script.Parent.waitingChair3.taken.Value = true
		else
			randomiseChair(clone)
		end
	end

	if random == 4 then
		if script.Parent.waitingChair4.taken.Value == false then
			clone.Humanoid:MoveTo(script.Parent.waitingChair4.Seat.Position, script.Parent.waitingChair4.Seat)
			clone.Humanoid.MoveToFinished:Wait()
			clone.Humanoid.Sit = true
			script.Parent.waitingChair4.taken.Value = true
		else
			randomiseChair(clone)
		end
	end
end
2 Likes
local parent = script.Parent

local function randomiseChair(clone)
	local human = clone:FindFirstChild("Humanoid")
	if not human then return end
	local random = math.random(1, 4)
	local chair = parent:FindFirstChild("waitingChair"..random)
	if not chair then return end
	local seat = chair:FindFirstChild("Seat")
	local taken = chair:FindFirstChild("taken")
	if not (seat and taken) then return end
	if taken.Value then
		human:MoveTo(seat.Position, seat)
		human.MoveToFinished:Wait()
		human.Sit = true
		taken.Value = true
	else
		randomiseChair(clone)
	end
end

This will support any number of chairs without requiring more code, all you’d need to do is change ‘4’.

1 Like

Notice that although you code works, it’s very long, try to look at what Forummer gave below :slight_smile: