Script is bugging when being fired multiple times

I am working on one of those ‘line simulator’ games, and I have it working pretty well. However, the script that allows players to cut the line is encountering a lot of bugs, and I can’t seem to figure out how to fix it.

The script determines whether a spot is taken using two values:
Player - Object Value
Bot - Bool Value

If the spot contains a bot instead of a player, it sets Bot to true and Player to nil.
But when this glitch happens, some empty spaces either have Bot set to true, or the Player value is still set to the player.

As you can see, there are a lot of bugs. Players and bots are skipping multiple lines, the player is getting moved back many spots when it shouldn’t happen, and the player is overlapping with the same spots as other bots/players.

Here is the script:

local ss = game:GetService("ServerStorage")
local ts = game:GetService("TweenService")

function updateBots()
	print("Bots")
	if #workspace.Bots:GetChildren() > 0 then
		for _, bot in pairs(workspace.Bots:GetChildren()) do
			local botSpot = bot.Spot
			if workspace.Spots:FindFirstChild(botSpot.Value) then
				local Spot = workspace.Spots[botSpot.Value]
				if Spot.Player.Value ~= nil then
					bot:Destroy()
				end
			end
		end
	else
		for i = 1, #workspace.Spots:GetChildren() do
			local newBot = ss.Bot:Clone()
			local newBotSpot = newBot.Spot
			local SSpot = workspace.Spots[i]
			local botBool = SSpot.Bot
			local playerObj = SSpot.Player
			if playerObj.Value == nil then
				botBool.Value = true
				newBotSpot.Value = tonumber(SSpot.Name)
				if SSpot then
					newBot:PivotTo(SSpot.Pos.CFrame)
					newBot.Parent = workspace.Bots
				end
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local checkSpot = 1
	local spot = Instance.new("IntValue", player)
	spot.Name = "Spot"
	for i = 1, #workspace.Spots:GetChildren() do
		local Spot = workspace.Spots[i]
		local botBool = Spot.Bot
		local playerObj = Spot.Player
		if playerObj.Value == nil then
			playerObj.Value = player
			botBool.Value = false
			spot.Value = tonumber(Spot.Name)
			break
		end
	end
	for _, Spot in pairs(workspace.Spots:GetChildren()) do
		local botBool = Spot.Bot
		local playerObj = Spot.Player
		if playerObj.Value == nil then
			botBool.Value = true
		end
	end
	player.CharacterAdded:Wait()
	local char = player.Character
	if workspace.Spots:FindFirstChild(spot.Value) then
		char:PivotTo(workspace.Spots[spot.Value].Pos.CFrame)
	end
	updateBots()
end)

game.Players.PlayerRemoving:Connect(function(player)
	local spot = player:WaitForChild("Spot")
	for _, Spot in pairs(workspace.Spots:GetChildren()) do
		if tonumber(Spot.Name) == spot.Value then
			local botBool = Spot.Bot
			local playerObj = Spot.Player
			botBool.Value = true
			playerObj.Value = nil
		end
	end
	updateBots()
end)

function cutLine(mode, player)
	print(mode)
	if mode == "Player" then
		local spot = player:WaitForChild("Spot")
		local cash = player:WaitForChild("Cash")
		local skipPrice = player:WaitForChild("skipAmount")
		local char = player.Character
		local nextSpot = workspace.Spots:FindFirstChild(spot.Value+1)
		if spot.Value+1 <= #workspace.Spots:GetChildren() then
			if char then
				local humanoid = char:WaitForChild("Humanoid")
				if nextSpot then
					if nextSpot.Bot.Value == true then
						local bot
						for _, bot in pairs(workspace.Bots:GetChildren()) do
							if bot.Spot.Value == tonumber(nextSpot.Name) then
								if workspace.Spots:FindFirstChild(spot.Value) then
									local moveBackPos = workspace.Spots:FindFirstChild(spot.Value).Pos
									spawn(function()
										ts:Create(bot.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = Vector3.new(moveBackPos.Orientation.X,moveBackPos.Orientation.Y,moveBackPos.Orientation.Z)*Vector3.new(math.rad(180),math.rad(180),math.rad(180))}):Play()
										wait(0.23)
										ts:Create(bot.HumanoidRootPart,TweenInfo.new(0.47,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = moveBackPos.Position}):Play()
										if bot:FindFirstChild("Animate") then
											game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(bot.Animate, true)
										end
										wait(0.54)
										if bot:FindFirstChild("Animate") then
											game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(bot.Animate, false)
										end
										ts:Create(bot.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = moveBackPos.Orientation}):Play()
									end)
									local newBotSpot = workspace.Spots:FindFirstChild(bot.Spot.Value-1)
									if newBotSpot then
										newBotSpot.Bot.Value = true
										newBotSpot.Player.Value = nil
									end
									bot.Spot.Value -= 1
								end
							end
						end
					elseif nextSpot.Bot.Value == false and nextSpot.Player.Value ~= nil then
						local nextPlayer = nextSpot.Player.Value
						local nextChar = nextPlayer.Character
						local Humanoid = nextChar:WaitForChild("Humanoid")
						if workspace.Spots:FindFirstChild(spot.Value) then
							local moveBackPos = workspace.Spots:FindFirstChild(spot.Value).Pos
							spawn(function()
								ts:Create(nextChar.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = Vector3.new(moveBackPos.Orientation.X,moveBackPos.Orientation.Y,moveBackPos.Orientation.Z)*Vector3.new(math.rad(180),math.rad(180),math.rad(180))}):Play()
								wait(0.23)
								ts:Create(nextChar.HumanoidRootPart,TweenInfo.new(0.47,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = moveBackPos.Position}):Play()
								if nextChar:FindFirstChild("Animate") then
									game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(nextChar.Animate, true)
								end
								wait(0.54)
								if nextChar:FindFirstChild("Animate") then
									game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(nextChar.Animate, false)
								end
								ts:Create(nextChar.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = moveBackPos.Orientation}):Play()
							end)
							local newBotSpot = workspace.Spots:FindFirstChild(nextPlayer.Spot.Value-1)
							if newBotSpot then
								newBotSpot.Bot.Value = true
								newBotSpot.Player.Value = nil
							end
							nextPlayer.Spot.Value -= 1
						end
					end
				end
				if player then
					nextSpot.Bot.Value = false
					nextSpot.Player.Value = player
					spawn(function()
						wait(0.23)
						ts:Create(char.HumanoidRootPart,TweenInfo.new(0.47,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = nextSpot.Pos.Position}):Play()
						if char:FindFirstChild("Animate") then
							game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(char.Animate, true)
						end
						wait(0.54)
						if char:FindFirstChild("Animate") then
							game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(char.Animate, false)
						end
						ts:Create(char.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = nextSpot.Pos.Orientation}):Play()
						wait(0.31)
						game.ReplicatedStorage.Remotes.Client.ReturnCutLineClient:FireClient(player, true)
					end)
				else
					print("No player found")
				end
				local newSpot = workspace.Spots:FindFirstChild(spot.Value+1)
				if newSpot then
					nextSpot.Bot.Value = false
					nextSpot.Player.Value = player
				end
				spot.Value += 1
			else
				game.ReplicatedStorage.Remotes.Client.ReturnCutLineClient:FireClient(player, false)
				cash.Value += skipPrice.Value
			end
		end
	elseif mode == "Bot" then
		if player.Parent == workspace.Bots then
			local spot = player.Spot
			local nextSpot = workspace.Spots:FindFirstChild(spot.Value+1)
			if nextSpot then
				local Player = nextSpot.Player.Value
				local Bot = nextSpot.Bot
				if Player then
					local nextChar = Player.Character
					local moveBackPos = workspace.Spots:FindFirstChild(spot.Value).Pos
					spawn(function()
						ts:Create(nextChar.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = Vector3.new(moveBackPos.Orientation.X,moveBackPos.Orientation.Y,moveBackPos.Orientation.Z)*Vector3.new(math.rad(180),math.rad(180),math.rad(180))}):Play()
						wait(0.23)
						ts:Create(nextChar.HumanoidRootPart,TweenInfo.new(0.47,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = moveBackPos.Position}):Play()
						if nextChar:FindFirstChild("Animate") then
							game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(nextChar.Animate, true)
						end
						wait(0.54)
						if nextChar:FindFirstChild("Animate") then
							game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(nextChar.Animate, false)
						end
						ts:Create(nextChar.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = moveBackPos.Orientation}):Play()
					end)
					local newBotSpot = workspace.Spots:FindFirstChild(Player.Spot.Value)
					if newBotSpot then
						newBotSpot.Bot.Value = true
						newBotSpot.Player.Value = nil
					end
					spawn(function()
						wait(0.23)
						ts:Create(player.HumanoidRootPart,TweenInfo.new(0.47,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = nextSpot.Pos.Position}):Play()
						if player:FindFirstChild("Animate") then
							game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(player.Animate, true)
						end
						wait(0.54)
						if player:FindFirstChild("Animate") then
							game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(player.Animate, false)
						end
						ts:Create(player.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = nextSpot.Pos.Orientation}):Play()
						wait(0.31)
					end)
					Player.Spot.Value -= 1
					local PlayerSpot = workspace.Spots:FindFirstChild(Player.Spot.Value)
					if PlayerSpot then
						PlayerSpot.Bot.Value = false
						PlayerSpot.Player.Value = Player
					end
				elseif not Player and Bot.Value == true then
					local Player
					for _, v in pairs(workspace.Bots:GetChildren()) do
						if v.Spot.Value == spot.Value+1 then
							Player = v
						end
					end
					if Player then
						local moveBackPos = workspace.Spots:FindFirstChild(spot.Value).Pos
						spawn(function()
							ts:Create(Player.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = Vector3.new(moveBackPos.Orientation.X,moveBackPos.Orientation.Y,moveBackPos.Orientation.Z)*Vector3.new(math.rad(180),math.rad(180),math.rad(180))}):Play()
							wait(0.23)
							ts:Create(Player.HumanoidRootPart,TweenInfo.new(0.47,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = moveBackPos.Position}):Play()
							if Player:FindFirstChild("Animate") then
								game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(Player.Animate, true)
							end
							wait(0.54)
							if Player:FindFirstChild("Animate") then
								game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(Player.Animate, false)
							end
							ts:Create(Player.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = moveBackPos.Orientation}):Play()
						end)
						local newBotSpot = workspace.Spots:FindFirstChild(Player.Spot.Value)
						if newBotSpot then
							newBotSpot.Bot.Value = true
							newBotSpot.Player.Value = nil
						end
						spawn(function()
							wait(0.23)
							ts:Create(player.HumanoidRootPart,TweenInfo.new(0.47,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = nextSpot.Pos.Position}):Play()
							if player:FindFirstChild("Animate") then
								game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(player.Animate, true)
							end
							wait(0.54)
							if player:FindFirstChild("Animate") then
								game.ReplicatedStorage.Remotes.Client.WalkingAnimationCutLine:FireAllClients(player.Animate, false)
							end
							ts:Create(player.HumanoidRootPart,TweenInfo.new(0.31,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Orientation = nextSpot.Pos.Orientation}):Play()
							wait(0.31)
						end)
						Player.Spot.Value -= 1
						local BotSpot = workspace.Spots:FindFirstChild(Player.Spot.Value)
						if BotSpot then
							BotSpot.Bot.Value = true
							BotSpot.Player.Value = nil
						end
					end
				end
			end
		end
	end
end

game.ReplicatedStorage.Remotes.Server.CutLine.OnServerEvent:Connect(function(player, debounce)
	if debounce == false then
		local spot = player:WaitForChild("Spot")
		local cash = player:WaitForChild("Cash")
		local skipPrice = player:WaitForChild("skipAmount")
		if cash.Value >= skipPrice.Value then
			cash.Value -= skipPrice.Value
			cutLine("Player", player)
		else
			game.ReplicatedStorage.Remotes.Client.ReturnCutLineClient:FireClient(player, false)
		end
	end
end)

while true do
	wait(0.5)
	if #workspace.Bots:GetChildren() > 0 then
		local randomBot = workspace.Bots:GetChildren()[math.random(1,#workspace.Bots:GetChildren())]
		cutLine("Bot", randomBot)
	end
end

The script is large and it’s hard to find what the issue is. I think it’s a problem when the function is fired multiple times at the same time.

If anyone can help me out with these glitches, I’d really appreciate it!