1 car spawn per person

  1. What do you want to achieve?
    -each player generates 1 only vehicle and that if he wants to generate 1 and already has one generated this will be eliminated

  2. What is the issue?

  1. What solutions have you tried so far?
  • so far none
    “Script Spawn car”
    script.Parent.MouseButton1Click:connect(function(GetCar)
    Mod = game.ServerStorage.Camaro --Camaro is the vehicle you are trying to spawn
    clone = Mod:clone()
    clone.Parent = workspace
    clone:MakeJoints()
    end)

Please put your code in code blocks, and indent your code!


local PlayerVehicleArray = {}

script.Parent.MouseButton1Click: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)

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

This should work, but I wrote it in the editor, so, if my code doesn’t work, reply with the error and I will fix it!

Edit: fixed an error where I forgot to include closing parenthesis

3 Likes

For a faster search, you could also use dictionaries. So instead of table.insert(PlayerVehicleArray,{player,clone}), you could write PlayerVehicleArray[player] = clone. By doing so, you also do not need a separate function as well, but your code is basically what you need.

2 Likes

You can’t call player from MouseButton1Click, also, if this was a local script, it wouldn’t work due to filtering enabled.

1 Like

I was assuming this was a ClickDetector. If it isn’t, I’d hope he changes it.

Edit: His spawn may have been a GUI, but even so, he would need to remote to the server.

ClickDetector would use InstanceClickDetector.MouseClick:Connect(function() exchange of MouseButton1Click

2 Likes

You should use Remote events, firing event from client with vehicle name, and serversided clone the car,
parent it , and name it like

Player.Name.."-Car"

Then, every time you spawn, check if there’s a model on workspace named Player.Name…“-Car” and destroys it if it exixts

You can just store it in a table…

local Cars = {}

--when they want to spawn a car
if Cars[player] then destroyVehicle(Cars[player]) end
Cars[player] = spawnVehicle(data)

--when they leave the game 
if Cars[player] then destroyVehicle(Cars[player]) end
--!IMPORTANT! to prevent memory leaks
Cars[player] = nil

Judging from what you said you could do this:

local myCar = game.Lighting.Car1 -- It can be where ever you want it to be stored

local currentCar = true
script.Parent.MouseButton1Click:Connect(function()
if currentCar == true then
local cloneCar = myCar:Clone()
cloneCar.Parent = workspace
currentCar = false
end
end)

No, that would not replicate and it should not be done locally anyways. You need to use Remotes to ask the server to spawn it. Also, never store anything in the Lighting service, theres like 5 other places which are designed for storing objects.

Yeah I know that but its because i had something in lighting at that time

This will solve your issue. Please mark me as solution if it works.

Step 1: Create a RemoteEvent in ReplicatedStorage, and name it CarSpawn.
Step 2: Put this code in a LocalScript inside your GuiButton that spawns the car:


script.Parent.MouseButton1Click:Connect(function(player)
 game.ReplicatedStorage.CarSpawn:FireServer()
end

Step 3: Put this code in a server Script in ServerScriptService:


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

If this doesn’t work, reply with your error, I’ll help you fix it. I edited the code I created from earlier to fit the Client-Server model standard with RemoteEvents. If it does work, though, please mark me as solution. Thanks!

3 Likes

Hey, sorry for the wayyyy to late reply, i have been looking for a script that similar to this
but seems to not work for me (some stuff is flagged red) . And also, is this script deletes cars after player summoning new one?