How do I update UI

I need help Updating my UI using this script. So I want it to add a new UI frame every time something is added to the Inventory folder inside the player, but whenever a child is added to said folder you must leave and rejoin to update it.

I’ve tried adding a .ChildAdded function before the for statement, but then it doesn’t update the UI until a child is added to the folder. For instance, when I join the game, and a child is in the Inventory folder it doesn’t show the current children on the UI until a child is added.

All help is appreciated!

local plr = game.Players.LocalPlayer
local Inventory = plr:WaitForChild("Inventory")
local UIdesc = script.Parent.Parent.ItemInfo.Desc
local Name = script.Parent.Parent.ItemInfo.ToolName


for _, tool in pairs(Inventory:GetChildren()) do
	local Btn = script.Parent.Parent.SwordButton:Clone()
	Btn.Parent = script.Parent
	Btn.Name = tool.Name
	Btn.Visible = true
	
	Btn.MouseButton1Click:Connect(function()
		UIdesc.Text = tool.Desc.Value
		Name.Text = tool.Name
	end)
end

Idk how to explain how to solve this but I know how to fix it. Try this:

local plr = game.Players.LocalPlayer
local Inventory = plr:WaitForChild("Inventory")
local UIdesc = script.Parent.Parent.ItemInfo.Desc
local Name = script.Parent.Parent.ItemInfo.ToolName

local function IdkWhatToNameThis(tool)
	if not tool:IsA("Tool") then return end
	if script.Parent:FindFirstChild(tool.Name) then return end
	
	local Btn = script.Parent.Parent.SwordButton:Clone()
	Btn.Parent = script.Parent
	Btn.Name = tool.Name
	Btn.Visible = true

	Btn.MouseButton1Click:Connect(function()
		UIdesc.Text = tool.Desc.Value
		Name.Text = tool.Name
	end)
end

Inventory.ChildAdded:Connect(IdkWhatToNameThis)
for I, X in Inventory:GetChildren() do IdkWhatToNameThis(X) end
1 Like

Thanks for the help! I will get straight to trying this!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.