Hi, I currently have a remote event that clones a random model [from a folder] within ServerStorage and positions it above a part in the workspace labeled ‘CarSpawn’. I check if the player’s name matches the player who cloned the model then delete the car model when the player collides with the part ‘destroyCar’. This works as intended until there are multiple models at once and only the last cloned model can be destroyed, which isn’t my intention. There are no errors. If you could elaborate as to why this is occurring, that’d be greatly appreciated.
-- Server Script
local cars = game:GetService("ServerStorage"):FindFirstChild("Vehicles"):GetChildren() -- Car directory
local randomCar = cars[math.random(1, #cars)] -- Pick random vehicle
local destroyCar = game.Workspace.destroyCar
local remoteEvent = game.ReplicatedStorage.SpawnCar
local Players = game:GetService("Players")
local str = " scrapped their vehicle."
local function spawnCar(player)
carClone = randomCar:Clone() -- Clone the chosen vehicle
carClone.Parent = workspace
carOwner = Instance.new('StringValue', carClone) -- Insert StringValue into model
carClone.PrimaryPart.CFrame = game.Workspace.CarSpawn.CFrame + Vector3.new(0,5,0)
carOwner.Value = player.Name -- Set StringValue to player's name
-- print(carOwner.Value) -- For debug purposes
end
local function deleteCar(player)
if player and player.Parent:FindFirstChildOfClass("Humanoid") then
local Client = Players:GetPlayerFromCharacter(player.Parent)
if Client.Name == carOwner.Value then -- Check if client's name is the same as the StringValue in the model
-- print(carOwner.Value .. str) -- For debug purposes
carClone:Destroy()
end
end
end
remoteEvent.OnServerEvent:Connect(spawnCar)
destroyCar.Touched:Connect(deleteCar)
1.Make carClone and CarOwner Variable as local variables.
2. make a table dictionary called local spawncar = {}. put key as playername and value as carClone.
3. to delete the car, check if there is playername key in table dictionary, destroy it and set key as nil.
local cars = game:GetService("ServerStorage"):FindFirstChild("Vehicles"):GetChildren() -- Car directory
--local randomCar = cars[math.random(1, #cars)] -- Pick random vehicle
local destroyCar = game.Workspace.destroyCar
local remoteEvent = game.ReplicatedStorage.SpawnCar
local Players = game:GetService("Players")
local str = " scraped their vehicle."
local spawncar = {}
local function spawnCar(player)
local carClone = cars[math.random(1, #cars)]:Clone() -- Clone the chosen vehicle
local carOwner = Instance.new('StringValue') -- Insert StringValue into model
carClone.PrimaryPart.CFrame = game.Workspace.CarSpawn.CFrame + Vector3.new(0,5,0)
carOwner.Value = player.Name -- Set StringValue to player's name
carOwner.Parent = carClone
spawnCar[carOwner.Name] = carClone
carClone.Parent = workspace
-- print(carOwner.Value) -- For debug purposes
end
local function deleteCar(player)
if player and player.Parent:FindFirstChildOfClass("Humanoid") then
local Client = Players:GetPlayerFromCharacter(player.Parent)
if spawnCar[client.Name] then -- Check if client's name is the same as the StringValue in the model
-- print(carOwner.Value .. str) -- For debug purposes
spawnCar[client.Name]:Destroy()
spawnCar[client.Name] = nil
end
end
end
remoteEvent.OnServerEvent:Connect(spawnCar)
destroyCar.Touched:Connect(deleteCar)
local cars = game:GetService("ServerStorage"):FindFirstChild("Vehicles"):GetChildren() -- Car directory
--local randomCar = cars[math.random(1, #cars)] -- Pick random vehicle
local destroyCar = game.Workspace.destroyCar
local remoteEvent = game.ReplicatedStorage.SpawnCar
local Players = game:GetService("Players")
local str = " scraped their vehicle."
local spawncar = {}
local function spawnCar(player)
local carClone = cars[math.random(1, #cars)]:Clone() -- Clone the chosen vehicle
local carOwner = Instance.new('StringValue') -- Insert StringValue into model
carClone.PrimaryPart.CFrame = game.Workspace.CarSpawn.CFrame + Vector3.new(0,5,0)
carOwner.Value = player.Name -- Set StringValue to player's name
carOwner.Parent = carClone
spawncar[carOwner.Name] = carClone
carClone.Parent = workspace
-- print(carOwner.Value) -- For debug purposes
end
local function deleteCar(player)
if player and player.Parent:FindFirstChildOfClass("Humanoid") then
local Client = Players:GetPlayerFromCharacter(player.Parent)
if spawncar[client.Name] then -- Check if client's name is the same as the StringValue in the model
-- print(carOwner.Value .. str) -- For debug purposes
spawncar[client.Name]:Destroy()
spawncar[client.Name] = nil
end
end
end
remoteEvent.OnServerEvent:Connect(spawnCar)
destroyCar.Touched:Connect(deleteCar)
local cars = game:GetService("ServerStorage"):FindFirstChild("Vehicles"):GetChildren() -- Car directory
--local randomCar = cars[math.random(1, #cars)] -- Pick random vehicle
local destroyCar = game.Workspace.destroyCar
local remoteEvent = game.ReplicatedStorage.SpawnCar
local Players = game:GetService("Players")
local str = " scraped their vehicle."
local spawnedCars = {}
local function spawnCar(player)
local carClone = cars[math.random(1, #cars)]:Clone() -- Clone the chosen vehicle
local carOwner = Instance.new('StringValue') -- Insert StringValue into model
carClone.PrimaryPart.CFrame = game.Workspace.CarSpawn.CFrame + Vector3.new(0,5,0)
carOwner.Value = player.Name -- Set StringValue to player's name
carOwner.Parent = carClone
spawnedCars[carOwner.Name] = carClone
carClone.Parent = workspace
-- print(carOwner.Value) -- For debug purposes
end
local function deleteCar(player)
if player and player.Parent:FindFirstChildOfClass("Humanoid") then
local Client = Players:GetPlayerFromCharacter(player.Parent)
if spawnedCars[client.Name] then -- Check if client's name is the same as the StringValue in the model
-- print(carOwner.Value .. str) -- For debug purposes
spawnedCars[client.Name]:Destroy()
spawnedCars[client.Name] = nil
end
end
end
remoteEvent.OnServerEvent:Connect(spawnCar)
destroyCar.Touched:Connect(deleteCar)
local function deleteCar(player)
print("Test") -- does print
if player and player.Parent:FindFirstChildOfClass("Humanoid") then
local client = Players:GetPlayerFromCharacter(player.Parent)
if spawncar[client.Name] then -- Check if client's name is the same as the StringValue in the model
print("test") -- doesn't print
spawncar[client.Name]:Destroy()
spawncar[client.Name] = nil
end
end
end
local cars = game:GetService("ServerStorage"):FindFirstChild("Vehicles"):GetChildren() -- Car directory
--local randomCar = cars[math.random(1, #cars)] -- Pick random vehicle
local destroyCar = game.Workspace.destroyCar
local remoteEvent = game.ReplicatedStorage.SpawnCar
local Players = game:GetService("Players")
local str = " scraped their vehicle."
local spawncar = {}
local function spawnCar(player)
local carClone = cars[math.random(1, #cars)]:Clone() -- Clone the chosen vehicle
local carOwner = Instance.new('StringValue') -- Insert StringValue into model
carClone.PrimaryPart.CFrame = game.Workspace.CarSpawn.CFrame + Vector3.new(0,5,0)
carOwner.Value = player.Name -- Set StringValue to player's name
carOwner.Parent = carClone
spawncar[player.Name] = carClone
carClone.Parent = workspace
-- print(carOwner.Value) -- For debug purposes
end
local function deleteCar(player) print(player.Name)
if player and player.Parent:FindFirstChildOfClass("Humanoid") then
print(player.Parent.Name)
print(spawncar[player.Name])
local client = Players:GetPlayerFromCharacter(player.Parent)
if spawncar[client.Name] then -- Check if client's name is the same as the StringValue in the model
print("kkkkk") -- For debug purposes
spawncar[client.Name]:Destroy()
spawncar[client.Name] = nil
end
end
end
remoteEvent.OnServerEvent:Connect(spawnCar)
destroyCar.Touched:Connect(deleteCar)