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)
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)
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.
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?
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.