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

For i, v in pairs, does not add to my table ?

So first of all, my apologies if I have done something pretty stupid, I'm very so new to for loops and of course like other people I try to improve myself. So basically what I tried doing here is, I have a folder in replicated storage, and whenever I add a new tool there and when I play the game, that tool would automatically add to the table I have created, but it does not work could anyone help?

local r = game:GetService("ReplicatedStorage")
local tool = r.Saber
local tools = {tool.StarterSaber}

for i, v in pairs(tools) do
	if v:IsA("Tool") then
		wait()
		table.insert(tools, v)
		print(tools)
	end
end

2 answers

✓ Accepted answer

You can't insert an object into a table. Only the name. Meaning:

local r = game:GetService("ReplicatedStorage")
local tool = r.Saber
local tools = {tool.StarterSaber}
 
for i, v in pairs(tools) do
    if v:IsA("Tool") then
        wait()
        table.insert(tools, v.Name)
        print(tools)
    end
end

2 comments

0

oh makes sense thanks

1

You can add objects to tables

Unsure why half of this doesn't work, but I don't recommend all the unneeded variables

local repStorage = game:GetService("ReplicatedStorage")
local tools = repStorage:WaitForChild("Saber").StarterSaber or workspace.Folder --incase your directory isnt a thing

for i, v in pairs(tools) do
    if v:IsA("Tool") then
        table.insert(tools, v)
        print(tools)
    end
end

1 comment

0

No, you cannot use table.insert since there isnt any table. plus im not saying only one tool, everytime I add anything, and run the game. Script should read the model and add the tool I have added into the table.

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