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
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
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.
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
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.
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!
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?