The script does not find the second tool in the ScreenGUI

I make an inventory GUI and when another tool is added to the player’s inventory, its frame does not find by its index, although it is added to the GUI

this is just a small part of the code

local function update()
	gui.Tools:ClearAllChildren()
	for name, data in pairs(toolconfig) do
		for index, tool in ipairs(tools) do
			if tool.Name == name then
				local frame = frame:Clone()
				frame.Parent = gui.Tools
				frame.TextIndex.Text = index
				frame.TextName.Text = name	
				for i = index, 1, -1 do
					frame.Position += UDim2.fromScale(0.035, 0)
					gui.Tools:GetChildren()[i].Position -= UDim2.fromScale(0.035, 0) -- here is the problem
				end
			end
		end
	end
end

You might have to go into more detail, such as more of the code, and more details by what is happening.
Is this inventory system using the Roblox Backpack container, or is it totally its own thing?
How are the characters getting the item (adding to inventory)
Once added, how are you storing this information?

im making my own backpack system and storing all items in the array

here is more of my code

local userinput = game:GetService("UserInputService")
local tween = game:GetService("TweenService")
local remote = game:GetService("ReplicatedStorage").Backpack.RemoteEvent
local frame = game:GetService("ReplicatedStorage").Backpack.Tool
local player = game:GetService("Players").LocalPlayer
local toolconfig = require(game:GetService("ReplicatedStorage").Config.Tools)
local gui = player.PlayerGui.Inventory
local tweeninfo = TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local tools = {}

local keys  = {
	Enum.KeyCode.One,
	Enum.KeyCode.Two,
	Enum.KeyCode.Three,
	Enum.KeyCode.Four,
	Enum.KeyCode.Five,
	Enum.KeyCode.Six,
	Enum.KeyCode.Seven,
	Enum.KeyCode.Eight
}

userinput.InputBegan:Connect(function(input, event)
	for i = 1, #keys do
		if input.KeyCode == keys[i] and tools[i] then
			remote:FireServer(tools[i])
		end
	end
end)

remote.OnClientEvent:Connect(function(tool, bool)
	local color = bool and Color3.new(1, 1, 1) or not bool and Color3.new(0, 0, 0)
	for i,v in ipairs(tools) do
		if v == tool then
			local frame = gui.Tools:GetChildren()[i]
			tween:Create(frame, tweeninfo, {BackgroundColor3 = color}):Play()
		end
	end
end)

local function update()
	print(tools)
	local weight = 0	
	gui.Tools:ClearAllChildren()
	gui.Weight.Visible = #tools > 0 and true or #tools == 0 and false
	for name, data in pairs(toolconfig) do
		for index, tool in ipairs(tools) do
			if tool.Name == name then
				local frame = frame:Clone()
				frame.Parent = gui.Tools
				frame.TextIndex.Text = index
				frame.TextName.Text = name			
				weight += data.Weight			
				for i = index, 1, -1 do
					frame.Position += UDim2.fromScale(0.035, 0)
					gui.Tools:GetChildren()[i].Position -= UDim2.fromScale(0.035, 0)	
				end
			end
		end
	end
	gui.Weight.TextLabel.TextColor3 = weight > 7 and Color3.new(1, 0, 0) or weight < 7 and Color3.new(1, 1, 1)
	gui.Weight.TextLabel.Text = weight .. "/10"
end

local function add(child)
	if child:IsA("Tool") and not table.find(tools, child) then
		table.insert(tools, child)
		update()
	end
end

local function remove(child)
	if child:IsA("Tool") then
		if child.Parent == player.Character or child.Parent == player.Backpack then
			return
		end
		for i,v in ipairs(tools) do
			if child == v then
				table.remove(tools, i)
				update()
			end
		end
	end
end

player.Character.ChildAdded:Connect(add)
player.Backpack.ChildAdded:Connect(add)
player.Character.ChildRemoved:Connect(remove)
player.Backpack.ChildRemoved:Connect(remove)

RemoteEvents are for connecting Server and Client. For Client-Client connections, you should use a BindableEvent

i need remote event to equip items in my backpack on server

So are you saying the tool is not showing up in the GUI?
Is it showing up in the data?
Is the server getting the signal from the remote event?

I see you are using a module script for Config.Tools, are you getting the correct results from that list?

1 Like

Not sure if this is the problem, but its probably not good coding practice.
Here you declare a global for frame…

Then later, you are making local variables called frame

And even trying to assign the global frame to the local frame
image

Perhaps rename the global one to ‘Frame’ or something different, and see if that helps any.

1 Like

I just took this loop out of main loop

local function update()
	local weight = 0	
	local frames = gui.Tools:GetChildren()
	gui.Tools:ClearAllChildren()
	gui.Weight.Visible = #tools > 0 and true or #tools == 0 and false
	for name, data in pairs(toolconfig) do
		for index, tool in ipairs(tools) do
			if tool.Name == name then
				local frame = frame:Clone()
				frame.Parent = gui.Tools
				frame.TextIndex.Text = index
				frame.TextName.Text = name			
				weight += data.Weight
			end
		end
	end
	for i1 = #gui.Tools:GetChildren(), 1, -1 do
		for i2 = i1, 1, -1 do
			gui.Tools:GetChildren()[i1].Position += UDim2.fromScale(0.035, 0)
			gui.Tools:GetChildren()[i2].Position -= UDim2.fromScale(0.035, 0)
		end
	end
	gui.Weight.TextLabel.TextColor3 = weight > 7 and Color3.new(1, 0, 0) or weight < 7 and Color3.new(1, 1, 1)
	gui.Weight.TextLabel.Text = weight .. "/10"
end

2 Likes

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