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)
--// 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)
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
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?
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