Script overwriting issue

Hello I’ve been stuck on this issue for ~3 days already and it’s been bugging me.

local function InitializeWeapons()

    Module_FPS.SetupViewmodel()
    for Key, Item in pairs(Equipped) do
        GunObjects[Item] = Module_FPS:New(Item)
        print(GunObjects)

        ContextActionService:BindAction(Item, Equip, false, Key)
    end
    warn(GunObjects)

end

The second time the loop runs it over rides the data from the previous loop but doesn’t change the key, what I’m talking about:

Expected output:

-- 1st print
["B1908"] = {
["Name"] = "B1908",
etc...
}

-- 2nd print
["B1908"] = {
["Name"] = "B1908",
etc...
},
["MP18"] = {
["Name"] = "MP18",
etc...
}

Actual output:

-- 1st print
["B1908"] = {
["Name"] = "B1908",
etc...
}

-- 2nd print
["B1908"] = {
["Name"] = "MP18", -- !!!!! this changes on 2nd print (2nd time the loop runs)
etc...
},
["MP18"] = {
["Name"] = "MP18",
etc...
}

Hello!
Can you please show what the table “Equipped” is?
Thanks

No problem, here:

local Equipped = {
    [Enum.KeyCode.One] = "MP18",
    [Enum.KeyCode.Two] = "B1908",
}

If you’d like to see anything else just say!

Can you also send “GunObjects” please?

local GunObjects = {}

It’s just an empty table whenever script starts.

Judging by the code the problem seems to be in this line

GunObjects[Item] = Module_FPS:New(Item)

Module_FPS is probably indexing your table incorrectly
Can you show what New() does?

I’m not going to show everything because its >300 lines but I’ll show what I’m pretty sure is important here:


    --//Variables\\--
    local Object = Common:WaitForChild(Newobject):Clone() Object.Parent = workspace.Runtime
    local Name = Newobject
    self.Name = Name
-- way more variables here

-- other functions etc...

    return self
end

Is newobject set to the passed parameter (or in your case the Item variable?)

Yes, I printed the “Newobject” right after the :new() function and it prints:
B1908
MP18

Can you try setting the Name variable to Object.name?
After that can you also send the output?

Sure,
image
As you can see in the warn() it prints as if the Name is “MP18” but in the first time the loop runs and prints it has the name correct.