I am attempting to create a car spawner which spawns only 1 car for each player, if you try to spawn again, it destroys your old car and makes the old spawn active (aka the old spawn is no longer taken).
I have 2 spawns, and it works right the first 2 times, however after the 2nd time, when it’s time to destroy the 2nd car, it says that the carspawner is nil.
I’ve tried looking for many solutions for days, however I can’t seem to figure this out.
Client Script
local SpawnQueue = {}
local RS = game:GetService("ReplicatedStorage")
local events = RS.Events
local carSpawnEvent = events.CarSpawn
local CSpawners = workspace.CarSpawner.Spawns
local player = game:GetService("Players").LocalPlayer
local playerUI = player:WaitForChild("PlayerGui")
local carSpawnHolder = playerUI:WaitForChild("Carspawner")
local mainFrame = carSpawnHolder:WaitForChild("MainFrame")
local ScrollingFrame = mainFrame:WaitForChild("ScrollingFrame")
local template = carSpawnHolder:WaitForChild("Template")
local exit = mainFrame:WaitForChild("Exit")
carSpawnEvent.OnClientEvent:Connect(function(Player,Model,Cars)
CreateCarTemplates(Cars)
for i,Object in pairs(ScrollingFrame:GetChildren()) do
if Object:IsA("TextButton") then
local CarButton = Object
local MouseEvent
MouseEvent = CarButton.MouseButton1Click:Connect(function()
local FoundSpawnQueue = FindQueueSpawn()
if not Player.Character:FindFirstChild("TAKEN") then
if FoundSpawnQueue ~= nil then
local carTaken = Instance.new("StringValue")
carTaken.Parent = Player.Character
carTaken.Value = CarButton.Name
carTaken.Name = "TAKEN"
carSpawnEvent:FireServer(Player,FoundSpawnQueue,CarButton.Name)
end
else
removeFindQueueSpawn(FoundSpawnQueue)
print("already got a car")
local deleteCar = workspace:FindFirstChild("OWNER_"..Player.Name)
print(deleteCar)
deleteCar:Destroy()
print("Pew!")
Player.Character:FindFirstChild("TAKEN"):Destroy()
end
for _, Frame in pairs(ScrollingFrame:GetChildren()) do
if Frame:IsA("TextButton") then
Frame:Destroy()
end
end
mainFrame.Visible = false
MouseEvent:Disconnect()
end)
exit.MouseButton1Click:Connect(function()
mainFrame.Visible = false
for _, Frame in pairs(ScrollingFrame:GetChildren()) do
if Frame:IsA("TextButton") then
Frame:Destroy()
end
end
end)
end
end
mainFrame.Visible = true
end)
FindQueueSpawn = function()
for Data, spawnPoint in pairs(CSpawners:GetChildren()) do
if spawnPoint.Taken.Value == false then
spawnPoint.Taken.Value = true
return spawnPoint
end
end
return nil
end
removeFindQueueSpawn = function(removeTaken)
removeTaken.Taken.Value = false
end
function CreateCarTemplates(Cars)
for i,car in pairs(Cars:GetChildren()) do
if car:IsA("BasePart") then
local newButton = template:Clone()
newButton.Name = car.CarName.Value
newButton.Text = car.CarName.Value
newButton.Parent = ScrollingFrame
newButton.Visible = true
end
end
end
function CreateQueue()
for _, SpawnC in pairs(CSpawners:GetChildren()) do
table.insert(SpawnQueue, {SpawnInstance=SpawnC, Queue_Used = false})
end
end
CreateQueue()
Server Script
--// Variables
local carSpawners = {}
local RS = game:GetService("ReplicatedStorage")
local events = RS.Events
local carSpawnEvent = events.CarSpawn
local carSpawner = script.Parent
local cars = carSpawner.Cars
--// Functions
local function onButtonPressed(player)
carSpawnEvent:FireClient(player,player,carSpawner,cars)
end
local function spawnCar(Player,SpawnPoint,CarButton)
print(cars)
print(CarButton)
print(Player)
print(SpawnPoint)
local Car = cars:FindFirstChild(CarButton):Clone()
Car.Parent = workspace
Car.PlayerName.Value = Player.Name
Car.CFrame = SpawnPoint.CFrame
Car.Name = "OWNER_"..Player.Name
end
--// Initialization
carSpawner.Button.ClickDetector.MouseClick:Connect(function(player)
onButtonPressed(player)
end)
carSpawnEvent.OnServerEvent:Connect(function(Player,player,SpawnPoint,CarButton)
print(CarButton)
spawnCar(Player,SpawnPoint,CarButton)
end)