How to can make this with table.insert()

Hello, I was wondering how to make this:


local scores = {
	["Player1"] = 2048;
	["Ozzypig"] = 1337;
	["Shedletsky"] = 1250;
	["Builderman"] = 1000;  
}

with table.insert().

-Zombo

1 Like

Thanks! so much! I thought it would be more complicated!

1 Like

I got an error:

  13:42:16.396  Workspace.LeaderBoard.Main.SurfaceGui.Frame.Script:7: Expected identifier when parsing expression, got '['  -  Studio - Script:7

Oh, one moment, I think I wrote it wrong.

I am using a for loop like below: Would I do this?

local refreshTime = 15

local scores = {}

while wait(refreshTime) do
	for i, player in pairs(game.Players:GetPlayers()) do
		table.insert(scores, i, [player.Name] = player.leaderstats.Wins.Value)		
	end
end

without the while loop of course.

Try something like table[key] = value

but I need the player name and wins count.

Using your example code:

local refreshTime = 15

local scores = {}

while wait(refreshTime) do
	for i, player in pairs(game.Players:GetPlayers()) do
		scores[player.Name] = player.leaderstats.Wins.Value
	end
end
2 Likes

Oh yes, my bad, what @robloxcommunityplus said is right, if you want to make a new value, just do table[valuename] = value

I mixed up with table.insert for normal values sorry :sweat_smile:

Can’t you just index the player’s key?

scores[player.Name] = player.leaderstats.Wins.Value

Would work for your needs, and if you need to get the stuff, just get the player’s name to check and it automatically gives you the result

print(scores[player.Name])

Would return the wins of that player

1 Like

Okay. I will try this. I should have known it would be like inserting functions into a table. You simply write it in. Thank You.

You use an array of tables

local scores = {}
table.insert(scores, { name = "Player1", points = 2048 })
table.insert(scores, { name = "Builderman", points = 1000 })

-- find a user's points by their username
for key, value in ipairs(scores) do
    if value.name == "Builderman" then
        return value.points
    end
end

But in your case it would be OK to use the player’s name or userID as the key of the dictionary.

It works. However, how do I remove the values from the table?

It won’t let me use table.remove()

table.remove only works with the index of the thing in the table.

local fruits = {"Apple", "Bananana", "Pear"}

table.remove(fruits, 2)

print(fruits)

--output
Apple
Pear

It’s better to use a dictionary since it’s unneeded complexity

I meant removing items from an array. But I found out you just have to set the index to nil

1 Like

table.insert and table.remove are used in arrays.

Guys you can just do table[“Example”] = blablabla then you just added a dictionary?