Car spawning problem

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)
1 Like

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)

https://developer.roblox.com/en-us/articles/Collision-Filtering

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

Well in that case I’d recommend looking at Region3 then :thinking: Maybe it’d help you, since it relies on a certain area I believe?

well, i saw somebody using .touched that also could work.

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?

Could you try adding print() statements to it?

Do i need to add something extra to the script, to it to work? i don’t know a lot of scripting, and i don’t know when or not to add something.

Oh wait I’m dumb LOL I increased the CarLimit instead of CurrentCars

local Debounce = false
local CarLimit = 10
local CurrentCars = 0

script.Parent.MouseButton1Click:connect(function(GetCar)
	if Debounce == false and CurrentCars < CarLimit then
		Debounce = true
        CurrentCars += 1
		Mod = game.ServerStorage.Car
		clone = Mod:clone()
		clone.Parent = workspace
		clone:MakeJoints()
		wait(5)
		Debounce = false
	end
end)

Ok now try this

Still a sadly no, still cars spawns after that

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)

image

So far, it seems to no detect the amount of cars that there are. that might be the problem (i set max cars as 3)

(Replying to this 7 hours later whoops) Hm strange, when I get a chance I’ll go ahead & test it on Studio I guess

Nah, no problem. i also went to sleep lol.

image
Weird, it’s working on my side

Could I see where you put the script exactly?

well, its on a gui on workspace.

Weird, what about the explorer?

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.

This will fix

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

i would appreciate it if you could check it out.