Only the last cloned model can be destroyed

  1. 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.

  2. https://gyazo.com/0d63116d65f07ce9cc1fbf031fc89416 (the second player who spawned their car first is unable to destroy their car-model.) There shouldn’t be any correlation in sequential order.

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

Thank you.

I’d use region3 instead of the touch event since region3 can detect a character and a car and hence forth destroy the car

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.

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

1 Like

Hi, this returned attempt to index function with ‘Value’ on Line 16.

spawnCar[carOwner.Name] = carClone

Replace

with

spawncar[carOwner.Name] = carClone
1 Like

sorry, my mistake
replace spawnCar[carOwner.Name] = carClone
with this
spawncar[carOwner.Name] = carClone

oh wait, just replace
local spawncar = {}
with
local spawnCar = {}

this returns the same error i mentioned above

No, that just causes a conflict between a variable name and a function name

1 Like

Did you try my solution?

Replace

with

spawncar[carOwner.Name] = carClone

Yes, it returns the error “attempt to index function with (playername)” on Line 24 when colliding with the destroyCar part.

if spawnCar[client.Name] then

oh your right. sorry my mistake

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)

No errors, however the model isn’t destroyed when the player collides with the part ‘destroyCar’.

is the function deleteCar running ?
put print something inside

it’s not returning the second print()

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)

1 Like

It should be working now.
i made a mistake at spawncar[carOwner.Name] = carClone
instead of spawncar[player.Name] = carClone

1 Like