Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to shorten the length of this script, or make it more efficient?

Asked by 9 years ago

I'm trying to make my own inventory, so far so good. But to me, this script that updates the inventory seems very long, and inefficient. Is there any way I can improve it?

01local plr = game.Players.LocalPlayer
02local buttons = script.Parent.IFrame:GetChildren()
03local items = plr.Backpack:GetChildren()
04local iframe = script.Parent.IFrame
05 
06function updateinven()
07    for i = 1,#items do
08        if #items == 1 then
09            iframe.Button1.Visible = true
10            iframe.Button1.ItemName.Value = items[1].Name
11            iframe.Button1.Text = items[1].Name
12        elseif #items == 2 then
13            iframe.Button1.Visible = true
14            iframe.Button1.ItemName.Value = items[1].Name
15            iframe.Button1.Text = items[1].Name
View all 47 lines...

1 answer

Log in to vote
3
Answered by 9 years ago

Time to use a table loop! This should make it shorter:

01local plr = game.Players.LocalPlayer
02local buttons = script.Parent.IFrame:GetChildren()
03local items = plr.Backpack:GetChildren()
04local iframe = script.Parent.IFrame
05local buttons = {iframe.Button1, iframe.Button2, iframe.Button3, iframe.Button4}
06 
07function updateinven()
08    for i = 1,#items do
09        buttons[i].Visible = true
10        buttons[i].ItemName.Value = items[i].Name
11        buttons[i].Text = items[i].Name
12    end
13end
14 
15plr.Backpack.ChildAdded:connect(updateinven)
16plr.Character.ChildAdded:connect(updateinven)
1
tankz!!1 Operation_Meme 890 — 9y
0
No problem! iconmaster 301 — 9y
Ad

Answer this question