Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Gingerely 245 Moderation Voter
4 years ago

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

Log in to vote
0
Answered by 4 years ago

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
0
oh makes sense thanks Gingerely 245 — 4y
1
You can add objects to tables hiimgoodpack 2009 — 4y
Ad
Log in to vote
0
Answered by
0_2k 496 Moderation Voter
4 years ago

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
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. Gingerely 245 — 4y

Answer this question