Find previous value in Changed function

Hello Developers,

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

Any help would be appreciated.

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

The outcome is still the same, I just need to find the previous value of the old car.

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.