i have a script that spawns cars, however everytime i test after some cars spawn studio entirely freezes.
ive tried removing tons of stuff to see what the issue was, but i couldnt find anything useful.
from what ive gathered its the car being spawned and nothing else
the actual car model doesnt have much parts and is all welded, so i don’t see what would be the issue here
i tried running a game and manually spam cloning a car, but the game didn’t crash so im really lost here
i had made a script like this a long time ago (albeit sloppier) but it never crashed, and im using the same car model from that script (with the linear velocity too)
whenever you do this while in the actual game, the server freezes up and then eventually you will get kicked cause of “internet connection”
im not sure what the exact cause is, but i was able to stop it by adding a wait that was around 7 seconds each car spawn, and i had a print statment whenever a car spawns, and it seemed to always crash whenever the 3rd car had spawned
heres the script
local carBase = game:GetService("ServerStorage").gamestuff.assets.cityCar
local module = require(game:GetService("ReplicatedStorage").modules.mainModule)
local carfolder = script.Parent.cars
local debris = game:GetService("Debris")
local carDMG = 1.1
local spawnpoints = {}
for i, v in pairs(script.Parent.spawnpoints:GetChildren()) do
if v and v:IsA("ObjectValue") and v.Value then
for i, a in pairs(v:GetChildren()) do
if a:IsA("BasePart") then
table.insert(spawnpoints, a)
end
end
end
end
local carHitCooldowns = {}
local hitCooldown = {}
local cars = {}
local function sendCar()
script.sendcar:Fire()
end
script.sendcar.Event:Connect(function()
if #spawnpoints > 0 then
local index = math.random(1,#spawnpoints)
local spawnpoint = spawnpoints[index]
if spawnpoint and spawnpoint:IsA("BasePart") then
table.remove(spawnpoints, index)
local destroyPart:BasePart = spawnpoint.Parent.Value
local car = carBase:Clone()
local color = Color3.fromRGB(math.random(0,255*100)/100,math.random(0,255*100)/100,math.random(0,255*100)/100)
for i, v in pairs(car:GetChildren()) do
if v:IsA("BasePart") then
if v.Name == "color" or v == car.PrimaryPart then
v.Color = color
end
end
end
local line = car.MAIN.Attachment0.LinearVelocity
local distance = (spawnpoint.Position-destroyPart.Position).Magnitude
local position = spawnpoint.CFrame*CFrame.new(0,0,-distance)
car.MYDESTROYPART.Value = destroyPart
car.MYSPAWNPART.Value = spawnpoint
car:PivotTo(spawnpoint.CFrame*CFrame.new(0,0,-19))
car.Parent = carfolder
table.insert(cars, car)
car.MAIN:SetNetworkOwner(nil)
line.LineDirection = CFrame.lookAt(line.Parent.WorldPosition, position.Position).LookVector
line.LineVelocity = 60
car.MAIN.drive:Play()
car.MAIN.closedrive:Play()
carHitCooldowns[car] = {}
car.hitTouchDetect.Touched:Connect(function(hit)
if hit and hit:IsDescendantOf(workspace) and not hit:IsDescendantOf(car) then
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and not table.find(hitCooldown, humanoid) then
table.find(hitCooldown, humanoid)
car.MAIN.hit:Play()
car.MAIN.honk:Play()
module.damage(nil, humanoid, carDMG*line.LineVelocity)
local cf = car.MAIN.CFrame*CFrame.Angles(math.rad(-65),0,0)
module.knockback(humanoid, line.LineVelocity*3, cf, 0.35)
task.wait(0.75)
local find = table.find(hitCooldown, humanoid)
if find then
table.remove(hitCooldown, find)
end
else
local carhit = hit.Parent
if carhit:IsA("Model") then
if carhit.Parent == carfolder then
local v1 = car.MAIN.AssemblyLinearVelocity.Magnitude
local v2 = carhit.MAIN.AssemblyLinearVelocity.Magnitude
if v2 <= v1-3 then
if not table.find(carHitCooldowns[car], carhit) then
table.insert(carHitCooldowns[car], carhit)
car.MAIN.hit:Play()
car.MAIN.honk:Play()
local bv = Instance.new("BodyForce")
bv.Name = "knockback"
bv.Force = car.MAIN.CFrame.LookVector*(line.LineVelocity*2)
bv.Parent = carhit.PrimaryPart
debris:AddItem(bv, 0.35)
task.wait(1.5)
local find = table.find(carHitCooldowns[car], carhit)
if find then
table.remove(carHitCooldowns[car], find)
end
end
end
end
end
end
end
end)
debris:AddItem(car, 30)
task.wait(1.15)
table.insert(spawnpoints, spawnpoint)
end
end
end)
for i, v in pairs(script.Parent.spawnpoints:GetChildren()) do
if v and v:IsA("ObjectValue") and v.Value then
local destroyPart:BasePart = v.Value
destroyPart.Touched:Connect(function(hit)
if hit and hit:IsDescendantOf(workspace) then
local car = hit.Parent
if car and car:IsA("Model") then
if car.Parent == carfolder then
local mydestroypart = car:FindFirstChild("MYDESTROYPART")
if mydestroypart and mydestroypart:IsA("ObjectValue") and mydestroypart.Value and mydestroypart.Value == destroyPart then
local find = table.find(cars, car)
if find then
table.remove(cars, find)
end
for i, v in pairs(cars) do
if not v then
table.remove(cars, i)
end
end
car:Destroy()
car = nil
end
end
end
end
end)
end
end
game:GetService("RunService").Heartbeat:Connect(function()
for i, car in pairs(cars) do
if car and car:IsDescendantOf(workspace) then
local myspawnpart = car:FindFirstChild("MYSPAWNPART")
local mydestroypart = car:FindFirstChild("MYDESTROYPART")
if myspawnpart and myspawnpart:IsA("ObjectValue") and myspawnpart.Value and myspawnpart.Value:IsA("BasePart") then
if mydestroypart and mydestroypart:IsA("ObjectValue") and mydestroypart.Value and mydestroypart.Value:IsA("BasePart") then
local line = car.MAIN.Attachment0.LinearVelocity
local distance = (myspawnpart.Value.Position-mydestroypart.Value.Position).Magnitude
local position = myspawnpart.Value.CFrame*CFrame.new(0,0,-distance)
line.LineDirection = CFrame.lookAt(line.Parent.WorldPosition, position.Position).LookVector
end
end
else
table.remove(cars, i)
end
end
end)
local function waitRandom(n1,n2)
local waitTime = math.random(n1*100,n2*100)/100
task.wait(waitTime)
end
task.wait(3)
while true do
if math.random(1,12) <=1 then
waitRandom(0.75,1.7)
elseif math.random(1,5) <= 2 then
waitRandom(0.15,0.75)
end
task.wait(0.05)
sendCar()
end
any help will be appreciated, ive been trying to fix this for hours