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

Why does this only work when the player joins?

So, no errors appear (seems to be a usual with my stuff), and yes, an item is being added to the inventory, but does not run the function. 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

6 comments

0

Put a print at the start of UpdateOldLabels.

0

The entire thing works, but only when the player joins, i need it to work when the inventory has an item added to it, you can see it there, but it doesn't work

0

wait for the backpack in player.

0

The backpack isn't the inventory, the inventory is a folder inside of the player to hold items

0

the backpack is definitely the inventory. Yet if you wanted the player to have the item after death you would then place it in the starter backpack

0

um are you sure? this is my code and I know exactly what it is. the inventory is a totally different thing than the backpack in this case.

1 answer

I wouldn't recommend just leaving it as if game.Players.PlayerAdded or plr.Inventory.ChildAdded then, because that only runs the script once. I would turn that into a function, and when a player is added or a value is changed, it fires the function.

Also: don't use ChildAdded on it's own for your inventory, because if an item is removed the function will not fire. I would use a `.ChildAdded and ChildRemoved.

Here is my suggested 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

function CreateImage()
    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

game.Players.PlayerAdded:connect(CreateImage)
plr.Inventory.ChildAdded:connect(CreateImage)
plr.Inventory.ChildRemoved:connect(CreateImage)

1 comment

0

this method does not work, I've tried it multiple times

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