Can someone explain how exactly to work with dictionaries?

I’ve already browsed the DevHub but I wanna see if anyone else can explain it a little better. This is an excerpt from a script I found:

local PlayerRunning = {}
sprintingPlayers[player.Name] = humanoid.WalkSpeed
		humanoid.WalkSpeed = humanoid.WalkSpeed * sprintModifier
	elseif state == "Ended" and sprintingPlayers[player.Name] then
		humanoid.WalkSpeed = sprintingPlayers[player.Name]
		sprintingPlayers[player.Name] = nil

How does the script know what the humanoid walkspeed is from the bracket “Player.Name” alone? Previously the dictionary or table (if there’s a difference between the 2, let me know) isn’t clearly defined.

Another time it’s used in the script is here:

local name = player.Name
player.Character.Humanoid.WalkSpeed = sprintingPlayers[name]

Here it’s setting the Humanoid’s WalkSpeed to the player’s name? What’s up with that? Nice detailed explanation of how they work would be nice.

This is setting a key-value pair, the player’s name being the key, mapped to the walk speed, which is the value. Indexing a table with a key returns the value, not the key.

So this is setting their walk speed to whatever number the player’s name is mapped to in the table.

So the bracket beside the table is either assigning a key to it or is used as a reference for a player property?

You can think of them like a drawer with infinite space, and each item in it is labeled.

For example, lets say you store a value with dictionary[Player.Name] = 10. The player is named “Player1”.
Now the dictionary looks like this:

{
     Player1 = 10
}

If you want to read a value in a dictionary you can either use a dot or brackets. Brackets can use variables, a dot cannot. eg

--Player.Name is "Player1"
print(dictionary.Player1) --10
print(dictionary[Player.Name]) --10

In your case sprintingPlayers would look like

{
    PlayerName = number
}

and sprintingPlayers[name] would then get the value in the dictionary, here used for the original walk speed.

Both. You can also use table.insert() for dictionaries as well.

Table.insert() and table[] does the same thing?

Yes, but if you try to table[] an array, it will give you an error.

table.insert(table, value) will treat the table as an array, making the table look like

--table.insert(table, "hi")
--table.OtherKey = "othervalue"
{
    [1] = "hi"
    OtherKey = "othervalue"
}

To add up, it’s not kinda like the same. table.insert is meant to insert a value to a table, while table[] on dictionaries do is index an existing key to get the key’s value or create a new key name.

1 Like

Ah I understand now, thank you all kindly for the splendid help.

This is a basic understanding of table btw. Try to read more from developer.roblox.com