Currently confused on cloning and destroying with a keybind toggle

Hi so I’ve been working on a game and I have it so when you press a key it spawns a model at you and stuff and it works, but I can’t make it so when you press the key again, it destroys the model.
I was trying to make it a toggle where you can go back and forth with pressing it but yeah I will always struggle trying to understand this stuff :confused:

local remote = game.ReplicatedStorage.BedEvent
local currentlyhasflower = false
local model = game.ReplicatedStorage.FlowerBed:Clone()

remote.OnServerEvent:Connect(function(plr, Player)
	if not currentlyhasflower then
		currentlyhasflower = true
		local humanoidRootPart = Player.Character.HumanoidRootPart
		local cFrame = model.PrimaryPart and model.PrimaryPart.CFrame
		local primaryPartCFrame = model:GetPrimaryPartCFrame()
		model:SetPrimaryPartCFrame(humanoidRootPart.CFrame)
		model.Parent = game.Workspace
	else
		model:Destroy()
		currentlyhasflower = false
	end
end)

Whenever an event is activated, it creates a new thread. In this case, you fire it for the first time; making a new thread, creating a model and spawning it into workspace. You fire it the second time; also making a new thread, creating a model(new reference) and spawning it into workspace. What you simply have to do is move ‘local model’ outside of the event like the variable currentlyhasflower.

That works but now I can’t make the model again :[
I need it so it can be like, made and destroyed if they press it again but idunno :{

This is occurring because, you’re creating a single clone of 'FlowerBed’ (line 3 - model) - then - when currentlyhasflower is true - you’re destroying that clone.

Instead, you need to move line 3 into the same code line as your if not currentlyhasflower then;

local remote = game.ReplicatedStorage.BedEvent
local currentlyhasflower = false
local flowerBed = game.ReplicatedStorage.FlowerBed
local model = nil

remote.OnServerEvent:Connect(function(plr, Player)
	if not currentlyhasflower then
		currentlyhasflower = true
		model = flowerBed:Clone() -- create a new clone of the FlowerBed
		local humanoidRootPart = Player.Character.HumanoidRootPart
		local cFrame = model.PrimaryPart and model.PrimaryPart.CFrame
		local primaryPartCFrame = model:GetPrimaryPartCFrame()
		model:SetPrimaryPartCFrame(humanoidRootPart.CFrame)
		model.Parent = game.Workspace
	else
		model:Destroy()
		currentlyhasflower = false
	end
end)

You could simplify this by removing the currentlyhasflower bool all together

local remote = game.ReplicatedStorage.BedEvent
local flowerBed = game.ReplicatedStorage.FlowerBed
local model = nil

remote.OnServerEvent:Connect(function(plr, Player)
	if not model then
		model = flowerBed:Clone() -- create a new clone of the FlowerBed
		local humanoidRootPart = Player.Character.HumanoidRootPart
		local cFrame = model.PrimaryPart and model.PrimaryPart.CFrame
		local primaryPartCFrame = model:GetPrimaryPartCFrame()
		model:SetPrimaryPartCFrame(humanoidRootPart.CFrame)
		model.Parent = game.Workspace
	else
		model:Destroy()
		model = nil
	end
end)
1 Like
-- we will use this table to save each players model
local playerModels = []

-- when the server gets a event call this function
game.ReplicatedStorage.BedEvent.OnServerEvent:Connect(function(player)
	-- try to get the model from the table
	local model = playerModels[player]
	-- if the model does not exist then clone the model if it does exist then destroy
	if model == nil then
		-- get the players rootPart CFrame
		local cFrame = player.Character.HumanoidRootPart.CFrame
		-- clone the model
		model = game.ReplicatedStorage.FlowerBed:Clone()
		-- position the model
		model:PivotTo(cFrame)
		-- place the model into the workspace
		model.Parent = workspace
		-- save the model to the tabel
		playerModels[player] = model
	else
		-- destroy the model
		model:Destroy()
		-- remove the model from the table
		playerModels[player] = nil
	end
end

-- so we don't get a memory leak if the players leaves the game make sure to delete there model
game.Players.PlayerRemoving:Connect(function(player)
	-- try to get the model from the table
	local model = playerModels[player]
	-- if the model is nil we don't need to do anything so lets exit out of the function
	if model == nil then return end
	-- destroy the model
	model:Destroy()
	-- remove the model from the table
	playerModels[player] = nil
end)
1 Like

Thanks I had a feeling it was that but I wasn’t sure, and it never occurred to me to try = nil :slight_smile: