-
I’m making a inventory system, and I want a script to fire every time a new tool is added to a player’s backpack.
-
I don’t know how too, and all solutions I’ve tried haven’t worked.
-
I’ve tried using a
while true do
loop, but it just spammed the function and cloned the same tool every time it ran.
Code
--Client sided
local player = game.Players.LocalPlayer
local backpack = player.Backpack
local frame = script.Parent:WaitForChild("ScrollingFrame") --Find our templates parent
local template = frame:WaitForChild("Template") --Find the template
for _, tool in pairs(backpack:GetChildren()) do --Loops through any tools in our tools folder
print(tool.Name.." is being added to the UI list.")
local newTemplate = template:Clone() --Clone our template everytime the loop completes
newTemplate.Name = tool.Name --Name the clone after the tool(VERY IMPORTANT)
newTemplate.ItemName.Text = tool.Name --Switch the textlabels text to the tools name
newTemplate.Visible = true --Make the button visible
newTemplate.Parent = frame --Parent the clone to our scrolling frame
local infoFrame = script.Parent.InfoFrame
newTemplate.MouseButton1Click:Connect(function() --When the button is clicked
print("Someone clicked the "..newTemplate.Name.." item.")
infoFrame.ToolName.Text = newTemplate.Name
infoFrame.ToolDrop.Visible = true
infoFrame.ToolName.Visible = true
infoFrame.ImageLabel.Visible = true
end)
local toolDrop = script.Parent.InfoFrame.ToolDrop
toolDrop.MouseButton1Click:Connect(function()
local tool2 = infoFrame.ToolName.Text
game.ReplicatedStorage.DropTool:FireServer(tool2)
end)
end