For Loop through Table then Clone Frame for Each Value

Hello, I need support on fixing an issue I am experiencing. I am trying to make it so for each value in a table a Frame is cloned and then the name of the Frame is the value in that table. However, when I unpack the table using table.unpack() it returns in a string with spaces: Flashlight Tool Tool2. I have tried splitting it using split() but goes back to the data. I am not sure how to fix this. I am using table.insert() to insert the values into the table.

(server)

Evt.GetTools.OnServerInvoke = function(player, id)
	local Tools = {}
	if ToolStorage:FindFirstChild(id) then
		for _, v in pairs(ToolStorage:FindFirstChild(id):GetChildren()) do
			if v:IsA("StringValue") then
				table.insert(Tools, v.Value)
			end
		end
		return Tools
	else
		return nil
	end
end

(local)

if value.Value ~= 0 then
		local tools = Evt.GetTools:InvokeServer(value.Value)
		if tools == nil then
		else
			print(table.unpack(tools))
			local Clone = script.Parent.ScrollingFrame.CloneMe:Clone()
			Clone.Parent = script.Parent.ScrollingFrame
			Clone.ToolName.Text = tools
			Clone.Visible = true
		end
	end

You can try this:

local Table = *table*

local frameToClone = *frame*

for i,v in ipairs(Table) do
-- what happens in each val

local clone = frameToClone:Clone()
clone.Parent = frameToClone

clone.Name = v
end

You can, after this script, change the parent, and do anything you’d like.

Just remember that the “clone” is inside the function where it happens for each value.

When I try to insert a value into that Table I get this error:

attempt to modify a readonly table
Evt.GetTools.OnServerInvoke = function(player, id)
	local Tools = table
	if ToolStorage:FindFirstChild(id) then
		for _, v in pairs(ToolStorage:FindFirstChild(id):GetChildren()) do
			if v:IsA("StringValue") then
				table.insert(Tools, v.Value)
			end
		end
		for i,v in ipairs(Tools) do
			print(v)
		end
	else
		return nil
	end
end

it’s because you just putted “table”

I said “table” so you could replace with your table. For example to “{}”

change “Tools” to the actual table

Sorry about that. I get the output of

Flashlight
Glock
Knife

Which is what I am expecting but this is a Invoke, I am wanting to return this information to the client. So when I would do return v it would only return one value since it is a for loop and would stop the loop continuing. Like this:

Evt.GetTools.OnServerInvoke = function(player, id)
	local Tools = {}
	if ToolStorage:FindFirstChild(id) then
		for _, v in pairs(ToolStorage:FindFirstChild(id):GetChildren()) do
			if v:IsA("StringValue") then
				table.insert(Tools, v.Value)
			end
		end
		for i,v in ipairs(Tools) do
			return v
		end
	else
		return nil
	end
end

I would most likely only get the output being “Flashlight” on the client.

Because when you return something the function ends immediately.

You shouldn’t return

If you want to return the table, then just do this:

Evt.GetTools.OnServerInvoke = function(player, id)
	local Tools = {}
	if ToolStorage:FindFirstChild(id) then
		for _, v in pairs(ToolStorage:FindFirstChild(id):GetChildren()) do
			if v:IsA("StringValue") then
				table.insert(Tools, v.Value)
			end
		end
		return Tools
	else
		return nil
	end
end

Figured it out on the client part, just had to do the loop on the client side instead of the server:

		local tools = Evt.GetTools:InvokeServer(value.Value)
		if tools == nil then
		else
			for i,v in ipairs(tools) do
				local Clone = script.Parent.ScrollingFrame.CloneMe:Clone()
				Clone.Parent = script.Parent.ScrollingFrame
				Clone.ToolName.Text = v
				Clone.Visible = true
			end
		end

Thank You!

No problem!

But did it work?

bla bla bla bla bla

Yep, just as I needed it to. I’ll go ahead and mark yours as the solution. Much appreciated.

No problem! Also thank you so much for the solution :smile:
Have a great day/night :heart:

1 Like

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