Plane spawner spawns 20 planes when it should only spawn 1

So, I am scripting a plane spawner system much like Naval Warfare And it was all going well until I tested it and it spawned 20 planes when i clicked once, I have no autoclickers and even tried to add a debounce, but nothing worked.

I have no for loops, while loops, repeats, or anything that should cause the code to run 20 times.

I also get no errors in the output (maybe because of the pcall, but other than that I’m not sure what is going on.)

My code:

--// Services
local ServerStorage = game:GetService("ServerStorage")

--// Variables
local Spawner = script.Parent
local SpawnPart = Spawner.PlaneSpawn
local SpawnRegion = Spawner.SpawnRegion
local SpawnCooldown = false

--// Functions
function SpawnPlane(SelectedPlane)
	local Success, Error = pcall(function()
		local PlaneToClone = ServerStorage.Vehicles[SelectedPlane]
		local Region = Region3.new(SpawnRegion.Position - (SpawnRegion.Size/2), SpawnRegion.Position + (SpawnRegion.Size/2))
		local PartsInRegion = workspace:FindPartsInRegion3(Region)
		
		for _, PartInRegion in pairs(PartsInRegion) do
			if PartInRegion:FindFirstAncestor("VehiclesInMap") then
				return
			else
				local PlaneClone = PlaneToClone:Clone()
				PlaneClone.Parent = workspace.VehiclesInMap
				PlaneToClone:SetPrimaryPartCFrame(CFrame.new(SpawnRegion.Position))
			end
		end
	end)
end

--// Main
SpawnPart.ClickDetector.MouseClick:Connect(function()
	if SpawnCooldown == false then
		SpawnCooldown = true
		SpawnPlane("Plane1")
		wait(5)
		SpawnCooldown = false
	end
end)

Any and all help is appreciated!

3 Likes
--// Services
local ServerStorage = game:GetService("ServerStorage")

--// Variables
local Spawner = script.Parent
local SpawnPart = Spawner.PlaneSpawn
local SpawnRegion = Spawner.SpawnRegion
local SpawnCooldown = false

--// Functions
function SpawnPlane(SelectedPlane)
	local Success, Error = pcall(function()
		local PlaneToClone = ServerStorage.Vehicles[SelectedPlane]
		local Region = Region3.new(SpawnRegion.Position - (SpawnRegion.Size/2), SpawnRegion.Position + (SpawnRegion.Size/2))
		local PartsInRegion = workspace:FindPartsInRegion3(Region)
		
		local a = false
		for _, PartInRegion in pairs(PartsInRegion) do
			if PartInRegion:FindFirstAncestor("VehiclesInMap") then
				return
			else
				a = true
				break
			end
		end
		if a then
			local PlaneClone = PlaneToClone:Clone()
			PlaneClone.Parent = workspace.VehiclesInMap
			PlaneToClone:SetPrimaryPartCFrame(CFrame.new(SpawnRegion.Position))
		end
	end)
end

--// Main
SpawnPart.ClickDetector.MouseClick:Connect(function()
	if SpawnCooldown == false then
		SpawnCooldown = true
		SpawnPlane("Plane1")
		wait(5)
		SpawnCooldown = false
	end
end)

It works better, but the first time I spawn it it spawns at 0,0,0 then the spot it needs to spawn.

Try setting the parent of the plane after you set the cframe:

local a = false
for _, PartInRegion in pairs(PartsInRegion) do
	if PartInRegion:FindFirstAncestor("VehiclesInMap") then
		return
	else
		a = true
		break
	end
end
if a then
	local PlaneClone = PlaneToClone:Clone()
	PlaneToClone:SetPrimaryPartCFrame(CFrame.new(SpawnRegion.Position))
	PlaneClone.Parent = workspace.VehiclesInMap
end

Attempted, it didn’t work, same thing happens

Maybe it’ll work if you remove the pcall function?

Testing. (Charsssssssssssssss)

Ok, it game me an error.
image

It is from a new line of code that sets its orientation to face forwards

PlaneToClone:SetPrimaryPartCFrame(SpawnRegion.CFrame.LookVector)

This should work:

PlaneToClone:SetPrimaryPartCFrame(SpawnRegion.CFrame)

The problem is that the look vector is the Vector 3 of the cframe.

Well that fixed the rotation issue, now its just the issue where it spawns at 0, 0, 0 first then the requested spot.

Can you be more clear? does the first plane you spawn locate at 0,0,0 and then the 2nd is fine? or does it appear at 0,0,0 then teleport to the requested spot?

No when I click kit once, it spawns at 0, 0, 0 and it stays there, when i click it again the plane spawns at the requested spot

Not sure what’s causing it, if you cant find a solution I guess just make it so the first plane spawned is instantly destroyed and replace with a new one? wont be particularly efficient or good practice but it… just works