Cars are being spawned twice when there are more than 2 players

When a player clicks the gui when theres another player, the player spawns 2 cars even though the player clicked once. As soon as they spawn 2, because of the destroying system. They get spawned back to the spawn location.

What is meant to happen is it’s meant to only spawn 1 car for the player, not 2.
I don’t know why it keeps happening.

game.ReplicatedStorage.SpawnCar1.OnServerEvent:Connect(function(player)
	print("Cloned")
	local car = game.ServerStorage.Red:Clone()
	car.Parent = game.Workspace
	car:PivotTo(car.WorldPivot + Vector3.new(0,10,0))
	car.Name = player.Name
	local humanoid = player.Character:FindFirstChild("Humanoid")
	local humanoidRoot = player.Character:FindFirstChild("HumanoidRootPart")

	if car.Name == player.Name then
		print("Got player car")
		humanoid.Sit = false
		humanoidRoot.CFrame = car.Seat.VehicleSeat.CFrame
		car.Seat.VehicleSeat:Sit(humanoid)
		script.Parent.Parent.Enabled = false
	elseif not humanoid.VehicleSeat then
		car.Seat.VehicleSeat:Sit(humanoid)    
	end


	print(car.Seat.VehicleSeat.Occupant)
	wait(3)

	while wait(1) do
		if car and car.Parent then 
			local occupant = car.Seat.VehicleSeat.Occupant
			if occupant == nil then
				car:Destroy()
				humanoidRoot.CFrame = game.Workspace.SpawnLocation.CFrame + Vector3.new(0,1,0)
			else
				print("Car not found/ already deleted")
			end
		end
	end
end)
2 Likes

It seems like there’s a glitch in the script that’s causing the game to spawn two cars instead of one when a player clicks the GUI button. It could be due to a problem with how the event is triggered or maybe there’s an error in the logic of the script itself.

To fix it, you might want to carefully go through the code and check for any mistakes or conditions that could be causing this issue. It could also be helpful to add some debugging messages to see what’s happening behind the scenes when the button is clicked.

I’m not seeing any issues with the script provided, perhaps the issue is found within the calling script. Are you able to provide the script that is calling the SpawnCar1 event?