Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to make this run more than once without a while loop?

This works, but only once, I need it to work when either of the events are fired, but I've tried multiple ways and none of them work.

Code:

local fram = script.Parent.back
local plr = script.Parent.Parent.Parent
function UpdateOldLabels(Parent)	
	for i,v in pairs(Parent:GetChildren()) do
		if v.Name:sub(1,4):lower() == "item" then
			local LineNumber = v.Name:sub(5)
			v.Name = "item"..tostring(tonumber(LineNumber) + 1)
			v.Position = v.Position + UDim2.new(0.02,0,0,0)
		end
	end
end

if game.Players.PlayerAdded or plr.Inventory.ChildAdded then
	for _,v in pairs(plr:WaitForChild("Inventory"):GetChildren()) do
		local item = Instance.new("ImageButton", fram)
		item.BackgroundTransparency = 1
		item.Name = "item1"
		item.Size = UDim2.new(.02, 0, .6, 0)
		item.Position = UDim2.new(-.02, 0, 0.15, 0)
		item.Image = "rbxassetid://"..tostring(v.itemPrev.Value)
		item.ZIndex = 2
		UpdateOldLabels(fram)
	end
end

2 comments

0

This is not at all how events work.

0

the script works perfectly fine,but only works once, and yes events can be used like that

1 answer

Make a function for them both, then make separate connection lines;

local fram = script.Parent.back
local plr = script.Parent.Parent.Parent

function UpdateOldLabels(Parent)    
	for i,v in pairs(Parent:GetChildren()) do
		if v.Name:sub(1,4):lower() == "item" then
			local LineNumber = v.Name:sub(5)
			v.Name = "item"..tostring(tonumber(LineNumber) + 1)
			v.Position = v.Position + UDim2.new(0.02,0,0,0)
		end
	end
end

--function for both
function doit()
	for _,v in next,fram:GetChildren() do 
		v:Destroy() 
	end
	for _,v in pairs(plr:WaitForChild("Inventory"):GetChildren()) do
		local item = Instance.new("ImageButton", fram)
		item.BackgroundTransparency = 1
		item.Name = "item1"
		item.Size = UDim2.new(.02, 0, .6, 0)
		item.Position = UDim2.new(-.02, 0, 0.15, 0)
		item.Image = "rbxassetid://"..tostring(v.itemPrev.Value)
		item.ZIndex = 2
		UpdateOldLabels(fram)
	end
end

--separate connection lines
game.Players.PlayerAdded:connect(doit)
plr.Inventory.ChildAdded:connect(doit)

1 comment

0

this does not work, it breaks the function to update the old labels, quite honestly don't know why

This discussion is preserved as it was. New answers, comments, and votes are closed.