After the 5th time my code runs, a error happens

Hiring Client:

local workersfolder = game.ReplicatedStorage.Workers
local waiter = workersfolder.Waiter
local plr = game.Players.LocalPlayer
local mainframe = plr.PlayerGui.WorkerGUI.Main.WaiterButton.WaitersFrame
local waiter1 = mainframe.Waiter1
local buy = waiter1.BuyButton
local leaderstats = plr:FindFirstChild("leaderstats")
local coins = leaderstats:FindFirstChild("Coins")
local WaiterCost = plr.WaiterCost
buy.MouseButton1Click:Connect(function()
	print("Client Value:"..coins.Value)
	if coins.Value >= WaiterCost.Value then
		local firstName = {
			'Noah', 'Sarah', 'Bob', 'Billy', 'Emma', 'Liam', 'Olivia', 'James', 'Sophia', 'Lucas', 'Ava', 'Mia',
			'Ethan', 'Isabella', 'Mason', 'Charlotte', 'Logan', 'Amelia', 'Jackson', 'Harper', 'Aiden', 'Evelyn', 'Lucas', 'Abigail',
			'Carter', 'Emily', 'Sebastian', 'Madison', 'Elijah', 'Elizabeth', 'Oliver', 'Scarlett', 'Jayden', 'Victoria', 'Henry', 'Chloe',
		}

		local fn = firstName[math.random(#firstName)]

		local lastname = {
			'Smith', 'Johnson', 'Brown', 'Davis', 'Wilson', 'Anderson', 'Garcia', 'Martinez', 'Miller', 'Taylor',
			'Thomas', 'Jackson', 'White', 'Harris', 'Young', 'Lee', 'Walker', 'Hall', 'Allen', 'King', 'Scott', 'Baker',
		}

		local ln = lastname[math.random(#lastname)]
		if ln == fn then -- IF THEY SOMEHOW ARE THE SAME
			fn = firstName[math.random(#firstName)]
			ln = lastname[math.random(#lastname)]
			print("They were the same, so they were rerolled")
		end

		game.ReplicatedStorage.Hiring:FireServer(1, fn, ln)
		waiter1.NameText.Text = fn .. " " .. ln
		buy.Visible = false
		waiter1.FrameClick.Visible = true
	end
end)

WorkerServer:

game.ReplicatedStorage.Hiring.OnServerEvent:Connect(function(plr,number,firstn,lastn)
	print("Sent to the server "..plr.Name.." with number "..number)
	local workersfolder = game.ReplicatedStorage.Workers
	local waiter = workersfolder.Waiter
	local mainframe = plr.PlayerGui.WorkerGUI.Main.WaiterButton.WaitersFrame
	local waiter1 = mainframe.Waiter1
	local buy = waiter1.BuyButton
	local leaderstats = plr:FindFirstChild("leaderstats")
	local coins = leaderstats:FindFirstChild("Coins")
	local WaiterCost = plr.WaiterCost
	local worker = waiter:Clone()
	if coins.Value >= WaiterCost.Value then
		coins.Value = coins.Value - WaiterCost.Value
		WaiterCost.Value = WaiterCost.Value + 250
		worker.Parent = workspace
		worker.Owner.Value = plr.Name
		worker.HumanoidRootPart.CFrame = plr.Character.HumanoidRootPart.CFrame
		worker.Name = plr.Name.."_waiter_"..number
		worker.Humanoid.DisplayName = "Waiter: "..firstn.." "..lastn
		worker.WorkerName.Value = firstn.." "..lastn
	end
end)
game.ReplicatedStorage.Firing.OnServerEvent:Connect(function(plr,worker,backAmount)
	worker:Destroy()
	local leaderstats = plr:FindFirstChild("leaderstats")
	local coins = leaderstats:FindFirstChild("Coins")
	coins.Value = coins.Value + backAmount
end)

FireClient:

local plr = game.Players.LocalPlayer
local mainframe = plr.PlayerGui.WorkerGUI.Main.WaiterButton.WaitersFrame
local overview = plr.PlayerGui.WorkerGUI.OverView
local fire = overview.FireButton
local numberOfWorker = fire.WorkerPicked
local leaderstats = plr:FindFirstChild("leaderstats")
local coins = leaderstats:FindFirstChild("Coins")
local WaiterCost = plr.WaiterCost

fire.MouseButton1Click:Connect(function()
	local workerSelected = plr.Name.."_waiter_"..numberOfWorker.Value
	local selectedWorker = workspace[workerSelected]
	local waiter = mainframe.Waiter1

	if selectedWorker then
		local returnAmount =	plr.WaiterCost.Value - 1.3 * 100 - 250
		print(returnAmount)
		coins.Value = coins.Value - returnAmount
		game.ReplicatedStorage.Firing:FireServer(selectedWorker,returnAmount)
		WaiterCost.Value = 250
		print("Cost: "..WaiterCost.Value)
		waiter.BuyButton.Visible = true
		waiter.NameText.Text = "Waiter "..numberOfWorker.Value
		overview.Visible = false
		numberOfWorker.Value = 0
		waiter.FrameClick.Visible = false
	else
		warn("Worker not found in the workspace.")
	end
end)

On the 5th time of hiring the worker and firing it, it will not spawn in the worker, but it will take my money>? Any idea how to fix

Thanks for the help, Your amazing

A likely cause is a variable scope issue (happens when the server runs out of waiter objects to clone because they are all placed in another part of the workspace).

The part of your WorkerServer script where worker objects are cloned and placed in workspace is inside an if then statement. After a certain number of iterations, there will be no more workers to clone, hence, the remaining cloned worker will be nil and your script will give an error.

One solution to fix this:

Instead of placing the cloned worker directly in the workspace, consider storing them in another location (like a folder or table). An example:

local workersInUse = {} -- create a table to store working workers

game.ReplicatedStorage.Hiring.OnServerEvent:Connect(function(plr,number,firstn,lastn)
	-- Same code here
	if coins.Value >= WaiterCost.Value then
		-- Same code here
		worker.Parent = workspace
        table.insert(workersInUse, worker) -- Store the worker in the table after assigning them to work
    end
end)

Now, before cloning a new worker, check if a worker is in use and return it to the worker folder if it’s not in use:

-- Before cloning a new worker, check if there's a worker in the workspace in use
for i, worker in ipairs(workersInUse) do
    -- Check if worker is still in use. If worker is not found or not in use, return it to the worker folder to be cloned again
    if worker.Parent == nil or worker.Owner.Value ~= plr.Name then
        worker.Parent = workersfolder
        table.remove(workersInUse, i) -- Remove this worker from the workersInUse table
    end
end

This way, you will always have worker objects to clone and place in workspace.