Well i found a script that spawns a car, but has a delay on it. and im still having problems with car bugging with each other, is there a way that it can be fixed? and also a limit on how much cars can be on the game instance? i dont want my game to be laggy. i couldn’t find anything related to this.
in others words, a way the cars not overlap with each other, but also have a limit on how many can be there.
local Debounce = false
script.Parent.MouseButton1Click:connect(function(GetCar)
if Debounce == false then
Debounce = true
Mod = game.ServerStorage.Car
clone = Mod:clone()
clone.Parent = workspace
clone:MakeJoints()
wait(5)
Debounce = false
end
end)
You could make Collision Groups for the Cars so they won’t collide with each other (If that’s what you’re saying), and for the limit you could just add a simple Number Variable and check for that
local Debounce = false
local CarLimit = 10
local CurrentCars = 0
script.Parent.MouseButton1Click:connect(function(GetCar)
if Debounce == false and CurrentCars < CarLimit then
Debounce = true
CarLimit += 1
Mod = game.ServerStorage.Car
clone = Mod:clone()
clone.Parent = workspace
clone:MakeJoints()
wait(5)
Debounce = false
end
end)
Collisions group seems a bit hard to me, i think it could be better if check if the zone where it spawns and if try to spawn a new one it will not, thanks anyways
Doesn’t work for me, i tried summoning 5 cars as put as limit (i put 10 as limit and 0 as current), i can spawn more than it, do i have to put something in current cars?
Hm, well print() statements are for debugging so & that code should work? Try lowering down the car limit, and if it still doesn’t work then try this debugged script:
local Debounce = false
local CarLimit = 10
local CurrentCars = 0
script.Parent.MouseButton1Click:connect(function(GetCar)
print("Event fired")
if Debounce == false and CurrentCars < CarLimit then
Debounce = true
CurrentCars += 1
print("Current Cars: "..CurrentCars)
Mod = game.ServerStorage.Car
clone = Mod:clone()
clone.Parent = workspace
clone:MakeJoints()
wait(5)
Debounce = false
else
print("Too much cars!")
end
end)
for i, v in pairs(script.Parent:GetChildren()) do
if v.Name == "Car" then
if i >= 5 then -- 5 is how many cars theres a limit to
print(i.. " Cars, cannot get no more cars")
return
end
end
end
This should fix the anti-lag problem if I understood correctly. This will check how many cars there are and using return that will stop any other code running from that line down. Tell me if you have any problems, I’ll try to help.
for i, v in pairs(script.Parent:GetChildren()) do
if v.Name == "Car" then
if i >= 5 then -- 5 is how many cars theres a limit to
print(i.. " Cars, cannot get no more cars")
return
end
end
end
print("HEY WORLDDDDD")
You can see here that if the cars are bigger than 5 (or whatever number) then it will not print “HEY WORLDDDDDDDDDDD”. No code will run from that line under.
I found another similar script that’s close to what i wanted, but it seems to be a little broken. This one is based around 1 player can only have 1 car spawned. and if player regenerates another it will delete the old one and spawn a new one, (from what i understand)
The stuff is
A remoteEvent called “CarSpawn” in ReplicatedStorage
A local script replacing the script spawning stuff
script.Parent.MouseButton1Click:Connect(function(player)
game.ReplicatedStorage.CarSpawn:FireServer()
end
A script located in ServerScriptService that makes the remoteEvent
local PlayerVehicleArray = {}
game.ReplicatedStorage.CarSpawn.OnServerEvent:Connect(function(player)
local PlayerVehicle = FindPlayerVehicle(player)
if PlayerVehicle then
PlayerVehicle:Destroy()
local Mod = game.ServerStorage.Camaro --Camaro is the vehicle you are trying to spawn
local clone = Mod:clone()
clone.Parent = game.Workspace
clone:MakeJoints()
table.insert(PlayerVehicleArray,{player,clone})
end
end)
function FindPlayerVehicle(player)
for i,v in pairs(PlayerVehicleArray) do
if v[1] == player then
return v[2]
break
end
end
return false --If a player doesn't have a vehicle attached to them, return false
end