suppose to display 3 buttons: "1", "2","3" but only show "3","3","1" cant figure out why
local gui = script.Parent local guii = gui.Frame local frame2 = gui.Frame1 local button1 = gui.button local Button = script.Parent.Configuration.a1 local slots = gui.Frame.ScrollingFrame.FrameH local pack local ply = gui.Parent.Parent local list1 = { "1", "2", "3" } local list2 = { "1", "2", "3" } --[[button1.MouseButton1Up:connect (function() a = Instance.new("TextLabel") a.Parent = game.Lighting:findFirstChild(ply.Name.."sPack") end)]] y = 0.3 x = 0.3 maxX = 0.9 maxY = 0.9 -- Y + 60 -- X + 40 function getMyPack() pack = game.Lighting:findFirstChild(ply.Name.."sPack") end function removeOldOutOfTheWay(location) for index, child in pairs(location) do if child.Name == "a1" then child:Remove() y = 0.3 x = 0.3 end end end function New() removeOldOutOfTheWay(slots:getChildren()) local lo = pack:getChildren() for index, child in pairs(lo) do neww = Button:Clone() neww.Position = UDim2.new(x, 0, y, 0) neww.TextLabel.Text = child.Name neww.Parent = slots x = x + 0.15 if x > 0.7 then y = y +0.015 x = 0.3 end end end getMyPack() New() pack.ChildAdded:connect(function() New() end) pack.ChildRemoved:connect(function() New() end) for i, v in pairs(list2) do n = gui.Configuration.Value:Clone() for g = 1,#list1 do for g = 1,#list2 do n.Name = list1[g] n.Value = list2[g] n.Parent = game.Lighting:findFirstChild(ply.Name.."sPack") end end end
There are some structural things you should change to clean things up.
In general, it is bad to make functions change global variables. They should instead return results. E.g., instead of defining local pack
at the top and making getMyPack
change pack
, it should just return that value:
function getMyPack() return game.Lighting:FindFirstChild(ply.Name .. "sPack") end .... local pack = getMyPack()
That said, you later use the expression game.Lightin .... .. "sPack")
, even though you have both a function to get you the value, and a variable!
Once again, we can eliminate the global variables x
and y
. They are only effectively changed by the function New
, so they should be local to that function.
Your loops at the bottom are just plain strange. I'm not sure why you have three loops.
If you want to add a new Configuration for each item in the lists, you should do something like this:
(You should also have the list definitions probably be near this rather than all the way at the top to avoid temptation for other places to touch them)
local nameList = { "1", "2", "3" } local valueList = { "1", "2", "3" } for i = 1, #nameList do local n = gui.Configuration.Value:Clone() n.Name = nameList[i] n.Value = valueList[i] n.Parent = pack end
Another minor detail is that you should probably :Clone()
the Button
you get at the beginning; you should also be using :Destroy()
instead of :Remove()
.
Your variable names all over the place aren't good. list1
doesn't describe what it is; a name like nameList
would be much better.
New
is similarly bad. UpdateSlots
is much more accurate and explanatory.
Note that you don't need to make a new function
when you're connecting to the ChildAdded and ChildRemoved events -- it will just call UpdateSlots
if you just give it UpdateSlots
.
Overall it would look something like this -- this is untested, you should make sure you can get here yourself:
local gui = script.Parent local Button = script.Parent.Configuration.a1:Clone() local slots = gui.Frame.ScrollingFrame.FrameH local ply = gui.Parent.Parent function getMyPack() return game.Lighting:FindFirstChild(ply.Name .. "sPack") end function removeOldOutOfTheWay(objects) for _, child in pairs(objects) do if child.Name == "a1" then child:Destroy() end end end function UpdateSlots() removeOldOutOfTheWay(slots:getChildren()) local x, y = 0.3, 0.3 local pack = getMyPack() local lo = pack:getChildren() for _, child in pairs(lo) do neww = Button:Clone() neww.Position = UDim2.new(x, 0, y, 0) neww.TextLabel.Text = child.Name neww.Parent = slots x = x + 0.15 if x > 0.7 then y = y + 0.015 x = 0.3 end end end local pack = getMyPack() pack.ChildAdded:connect( UpdateSlots ) pack.ChildRemoved:connect( UpdateSlots ) UpdateSlots() local nameList = { "1", "2", "3" } local valueList = { "1", "2", "3" } for i = 1, #nameList do local n = gui.Configuration.Value:Clone() n.Name = nameList[i] n.Value = valueList[i] n.Parent = pack end