Destroy Clone function when die?

Hey everyone I believe this is easy but I’m not sure. So if I equip an Item from my Inventory and die it clones itself in the inventory and I don’t want to delete it but stop it from cloning when dead I have 2 small scripts with clone in them can anyone refix the scripts?


First local script:

local InventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent
local itemFrame = script.Parent:FindFirstChild("ItemsFrame")

InventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
	if Value == true then

		local ItemButton = script.Parent.ItemsFrame.ItemButton:Clone()
		ItemButton.Visible = true
		ItemButton.Name = ItemName
		ItemButton.Text = ItemName
		ItemButton.Parent = itemFrame

		local inventoryGui = script.Parent.Parent
		local equipFrame = inventoryGui:WaitForChild("EquipFrame")
		local equipButton = equipFrame:WaitForChild("EquipButton")

		ItemButton.MouseButton1Click:Connect(function()
			equipFrame.Title.Text = ItemName
			equipFrame.Title.Visible = true
			equipButton.Visible = true
		end)
	end

end)










Second script:


local inventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent

game.Players.PlayerAdded:Connect(function(player)

	local inventory = player:WaitForChild("Inventory")

	local inventoryFrame = player.PlayerGui:WaitForChild("InventoryGui").InventoryFrame.ItemsFrame:GetChildren()

	inventory.ChildAdded:Connect(function(Item)
		inventoryEvent:FireClient(player, Item.Name, true)
	end)
end)

inventoryEvent.OnServerEvent:Connect(function(player, ItemName, Value, button)

	if Value == false then
		local SelectedItem = player.Inventory:FindFirstChild(ItemName)
		local backpack = player.Backpack:GetChildren()
		local stuff = player.Character:GetChildren()

		if #backpack == 0 and not player.Character:FindFirstChildWhichIsA("Tool") then
			button.Text = "Unequip"
			button.BackgroundColor3 = Color3.new(255,0,0)
			SelectedItem:Clone().Parent = player.Backpack
		else
			for i,v in ipairs(backpack) do
				button.Text = "Equip"
				button.BackgroundColor3 = Color3.new(0,255,0)
				v:Destroy()
			end
			for i, v in ipairs(stuff) do
				if v:IsA("Tool") then
					button.Text = "Equip"
					button.BackgroundColor3 = Color3.new(0,255,0)
					v:Destroy()
				end
			end
		end
	end
end)
1 Like

change

	if Value == true then

to

	if Value == true and Player.Character.Humanoid.Health > 0 then

make sure to put the local player in a variable (local Player = game:GetService("Players").LocalPlayer)

my bad i did not see the second script hold on

1 Like

my bad, but on the second script

inventoryEvent.OnServerEvent:Connect(function(player, ItemName, Value, button)

	if Value == false then
		local SelectedItem = player.Inventory:FindFirstChild(ItemName)
		local backpack = player.Backpack:GetChildren()
		local stuff = player.Character:GetChildren()

		if #backpack == 0 and not player.Character:FindFirstChildWhichIsA("Tool") then
			button.Text = "Unequip"
			button.BackgroundColor3 = Color3.new(255,0,0)
			SelectedItem:Clone().Parent = player.Backpack
		elseif player.Character.Humanoid.Health > 0 then
			for i,v in ipairs(backpack) do
				button.Text = "Equip"
				button.BackgroundColor3 = Color3.new(0,255,0)
				v:Destroy()
			end
			for i, v in ipairs(stuff) do
				if v:IsA("Tool") then
					button.Text = "Equip"
					button.BackgroundColor3 = Color3.new(0,255,0)
					v:Destroy()
				end
			end
		end
	end
end)

This may work out for you …

First Script
task.wait(3)
local inventoryEvent = game:GetService("ReplicatedStorage")
	:WaitForChild("Remotes"):WaitForChild("inventoryEvent")
local itemFrame = script.Parent:FindFirstChild("ItemsFrame")

inventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
	if Value and not itemFrame:FindFirstChild(ItemName) then
		local ItemButton = script.Parent.ItemsFrame.ItemButton:Clone()
		ItemButton.Visible = true
		ItemButton.Name = ItemName
		ItemButton.Text = ItemName
		ItemButton.Parent = itemFrame

		local inventoryGui = script.Parent.Parent
		local equipFrame = inventoryGui:WaitForChild("EquipFrame")
		local equipButton = equipFrame:WaitForChild("EquipButton")

		ItemButton.MouseButton1Click:Connect(function()
			equipFrame.Title.Text = ItemName
			equipFrame.Title.Visible = true
			equipButton.Visible = true
		end)
	end
end)
Second Script
local inventoryEvent = nil
game.Players.PlayerAdded:Connect(function(player)
	local inventory = player:WaitForChild("Inventory")
	inventory.ChildAdded:Connect(function(Item)
		if not inventory:FindFirstChild(Item.Name) then
			if(inventoryEvent)then
				inventoryEvent:FireClient(player, Item.Name, true)
			end
		end
	end)
end)

inventoryEvent = game:GetService("ReplicatedStorage")
 :WaitForChild("Remotes"):WaitForChild("inventoryEvent")

inventoryEvent.OnServerEvent:Connect(function(player, ItemName, Value, button)
	if Value == false then
		local SelectedItem = player.Inventory:FindFirstChild(ItemName)
		local backpack = player.Backpack:GetChildren()
		local stuff = player.Character:GetChildren()

		if #backpack == 0 and not player.Character:FindFirstChildWhichIsA("Tool") then
			button.Text = "Unequip"
			button.BackgroundColor3 = Color3.new(255, 0, 0)
			if SelectedItem then
				SelectedItem:Clone().Parent = player.Backpack
			end
		else
			for _, v in ipairs(backpack) do
				button.Text = "Equip"
				button.BackgroundColor3 = Color3.new(0, 255, 0)
				v:Destroy()
			end
			for _, v in ipairs(stuff) do
				if v:IsA("Tool") then
					button.Text = "Equip"
					button.BackgroundColor3 = Color3.new(0, 255, 0)
					v:Destroy()
				end
			end
		end
	end
end)

Good luck with your game!

Summary

In the First Script: Items won’t be visually duplicated in the player’s GUI because it checks if the button already exists before cloning.

In the Second Script: Items won’t be redundantly added to the Inventory because it checks whether the item is already there before firing the event to the client.

That’s what I was going for anyways … sadly I couldn’t test this.

Still not working? What was the error? I re-ordered a bit to account for loading time.

1 Like

Well uh they both sadly changed nothing but I believe it’s because of the first script this line:
because when I die it adds an Item Button in the Inventory isn’t there any way to like if humanoid destroy = clone = false or something?

oh wait lemme check just saw it

I tried it but now its not working like the other dude silver like can’t I just destroy the cloning when humanoid di?

you can make a data store that stores the tool in a Folder or Starterpack, that way you can use Player.CharacterAdded:Connect(function() and then for i, v in inventory:GetChildren() do

and btw, you will HAVE to… know how to use module scripts, life will be easier when you do so.

1 Like

2 Likes

uhh alright thx for the advice guess it’ll take a while…

a few weeks if you are dedicated to it :slight_smile: unlike me who took 4 monthes

1 Like