I have this tool spawner that once you click, it spawns the model. However, if I click it again, the model will spawn the same model from before. Here is this gif that will explain the whole situation.

(https://gyazo.com/f54b14c563546cf66bdfca84f847c314)

I want it where once you click again, it spawns a whole new model instead of the previous one.

Here is the script for the tool.

local TAXI_ASSET = 21322 


local PlayersService = game:GetService('Players')
local DebrisService = game:GetService('Debris')
local MyCharacter = nil
local MyTorso = nil
local Tool = script.Parent
local Handle = Tool:WaitForChild('Handle')
local Car = game:GetService('InsertService'):LoadAsset(TAXI_ASSET):GetChildren()[1]

local COOLDOWN_TIME = 0.1

local DeployedCar = nil

function OnTouched(hitPart)
	
end

function CreateOwnerTag()
	local ownerTag = Instance.new('ObjectValue')
	ownerTag.Value = PlayersService:GetPlayerFromCharacter(MyCharacter)
	ownerTag.Name = "Owner"
	return ownerTag
end

function SpawnCar()
	if MyTorso then
		local ownerTag = CreateOwnerTag()
		if DeployedCar then
			DeployedCar:Destroy()
		end
		local carClone = Car:Clone()
		ownerTag.Parent = carClone
		carClone.Parent = game.Workspace
		print(Car:GetModelCFrame().p)
		carClone:TranslateBy((MyTorso.CFrame.p - Car:GetModelCFrame().p) + Vector3.new(0,5,0) + MyTorso.CFrame.lookVector * 15)
		carClone:TranslateBy(Vector3.new(0, 30, 0))
		carClone:MakeJoints()
		DeployedCar = carClone
		
		DebrisService:AddItem(carClone, 60 * 30) -- 30 mins debris so they doesnt stay forever
	end
end

function OnActivated()
	if not Tool.Enabled then return end
	Tool.Enabled = false

	SpawnCar()
	wait(COOLDOWN_TIME)
	Tool.Enabled = true
end

function OnEquipped(mouse)
	Handle.Touched:connect(OnTouched)
	MyCharacter = Tool.Parent
	MyTorso = MyCharacter:WaitForChild('Torso')
end

Tool.Activated:connect(OnActivated)
Tool.Equipped:connect(OnEquipped)

2 comments

0

Do you want it to not destroy the old one?

0

Just remove this? " if DeployedCar then DeployedCar:Destroy() end"