I have this code here: table.insert(MainPlayerSkins,tostring(Player.UserId,SkinsData)
It inserts a table of player skins with the key being the player user id. I also have this line of code
for i, v in pairs(MainPlayerSkins) do
if i == tostring(Player.UserId) then
print("Should be removed")
table.remove(MainPlayerSkins,i)
print(MainPlayerSkins)
end
end
This removes the index of the player when they leave the game.
Strangely this doesnt work.
it prints “Should be removed” but never actually removes the index.
After the attempt of removing the index i print out the main player skin table called “MainPlayerSkins” but the index is still there like nothing happened?
Does anyone have any fix ive been trying to solve this issue for a few days now
Please help!
You’re converting the UserIDs to strings. 1 ~= “1”, in comparison logic or table indexing (hashing) logic. E.g.
local t = {}
t["1"] = "test"
for i = 1, 3 do
print(t[i]) --prints nil, nil, nil, because 1 ~= "1"
end
You can fix your code like this:
table.remove(MainPlayerSkins,tostring(i))
Do you have a good reason for converting to strings in the first place? Otherwise just use the UserID as a key without converting it:
MainPlayerSkins[Player.UserId] = SkinsData
...
function table.printkvtypes(t)
local strs = {}
for k, v in pairs(t) do
table.insert(strs, ("[%s %s]: %s %s"):format(type(k), tostring(k), type(v), tostring(v)))
end
print(table.concat(strs, ", "))
end
...
print("Before: ")
table.printkvtypes(MainPlayerSkins)
for userId, _ in pairs(MainPlayerSkins) do
if userId == Player.UserId then
print("Should be removed")
MainPlayerSkins[userId] = nil
end
end
print("After: ")
table.printkvtypes(MainPlayerSkins)