I’ve been wanting to make where the previous equipped car would be destroyed when the new car is equipped. For this I’ve been using .Changed as my function to get the new car but I’m not really sure how to get the previous equipped car destroyed.
If you have any questions, please ask me.
-- local player = game.Players.LocalPlayer
local primaryCar = player:WaitForChild("PrimaryCar")
local oldCar
if player.Character then
local newCar = game.ReplicatedStorage.Cars:FindFirstChild(primaryCar.Value.Name):Clone()
newCar.Parent = game.Workspace
newCar.Name = player.Name.."'s model car"
newCar:SetPrimaryPartCFrame(workspace.Garage:WaitForChild("CarTeleportPart").CFrame)
newCar.PrimaryPart.Anchored = true
player.PrimaryCar.Changed:Connect(function(EnewCar)
if newCar then
newCar:Destroy()
end
task.wait(0.5)
local newnewCar = EnewCar:Clone()
newnewCar.Parent = game.Workspace
newnewCar:SetPrimaryPartCFrame(workspace.Garage:WaitForChild("CarTeleportPart").CFrame)
newnewCar.PrimaryPart.Anchored = true
end)
end
might be as simple as just removing a simple ‘local’
-- local player = game.Players.LocalPlayer
local primaryCar = player:WaitForChild("PrimaryCar")
local oldCar
if player.Character then
local newCar = game.ReplicatedStorage.Cars:FindFirstChild(primaryCar.Value.Name):Clone()
newCar.Parent = game.Workspace
newCar.Name = player.Name.."'s model car"
newCar:SetPrimaryPartCFrame(workspace.Garage:WaitForChild("CarTeleportPart").CFrame)
newCar.PrimaryPart.Anchored = true
player.PrimaryCar.Changed:Connect(function(EnewCar)
if newCar then
newCar:Destroy()
end
task.wait(0.5)
newnewCar = EnewCar:Clone()
newnewCar.Parent = game.Workspace
newnewCar:SetPrimaryPartCFrame(workspace.Garage:WaitForChild("CarTeleportPart").CFrame)
newnewCar.PrimaryPart.Anchored = true
end)
end
local player = game.Players.LocalPlayer
local primaryCar = player:WaitForChild("PrimaryCar")
local oldCar
if player.Character then
local newCar = game.ReplicatedStorage.Cars:FindFirstChild(primaryCar.Value.Name):Clone()
newCar.Parent = game.Workspace
newCar.Name = player.Name.."'s model car"
newCar:SetPrimaryPartCFrame(workspace.Garage:WaitForChild("CarTeleportPart").CFrame)
newCar.PrimaryPart.Anchored = true
oldCar = newCar
player.PrimaryCar.Changed:Connect(function(EnewCar)
if oldCar then
oldCar:Destroy()
end
task.wait(0.5)
local newnewCar = EnewCar:Clone()
newnewCar.Parent = game.Workspace
newnewCar:SetPrimaryPartCFrame(workspace.Garage:WaitForChild("CarTeleportPart").CFrame)
newnewCar.PrimaryPart.Anchored = true
oldCar = newnewCar
end)
end
Found out the solution, I had to simply put oldCar = newnewCar