Dictionary and Player.Name help

  1. What do you want to achieve? Keep it simple and clear!

I want to insert multiple players names into a single dictionary where there values will be true or false.
Then i want to be able to change those individual values by the individual player pressing a button.

  1. What is the issue? Include screenshots / videos if possible!

When inserting the newPlayer.Name into a variable called name, the result in print is that the key in dictionary is actually only the word name. creating a wrong name for the key, also creating a individual key when i need a key per player.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I am following a tutorial from the book “The official Roblox guide, Coding with Roblox Lua” to do the insert to dictionary function but does not work.

Sample Code (not sure how to put in code format to read or is automatic) -
– code has been altered so names of variables and dictionaries are not exact.
– first part of question. how to add in dictionary the player name properly
dictionaryName = {
}

function establishPlayer(newPlayer)
local name = newPlayer.Name
dictionaryName.name = false
for i, v in pairs(dictionaryName) do – reads the players names every time added player joins. but the result is only printing the word “name” because of code dictionaryName.name does not use the variable name, is using the literal word name to insert the word “name” as a dictionary variable
print(i) – results in out put statement (13:55:34.626 name - Server)
end
end
Players.PlayerAdded:Connect(dictionaryName)
– end first part

– second part
– second part takes the errored dictionary value the word “name” is used instead of player’s name, but the code does change the value of the key from the dictionary. I will need to know how to use variable to look up dictionary variable (player name) to change there individual value.

game.ReplicatedStorage.HideTest1.OnServerEvent:Connect(function(player)
local name = player.Name – does this work?
if dictionaryName.name == false then
dictionaryName.name = true
else
dictionaryName.name = false
end
end)
– end second part
Would appreciate if in your responses used code as examples so i have exact answers. Thank you for your help. If you have questions i will answer. If this is fixed with your help i will post a response question with the uses of this dictionary key with value in its actual use in the script. Thanks again!

To add a new value to a dictionary when it’s name is stored inside a variable, you don’t use Dictionary.ValueName = Value but Dictionary[ValueName] = Value.

For code lines you use ```

@paodebatata4000 is right, try using this code:

dictionaryName = {
}

function establishPlayer(newPlayer)
local name = newPlayer.Name
dictionaryName[name] = false
for i, v in pairs(dictionaryName) do--reads the players names every time added player joins. but the result is only printing the word “name” because of code dictionaryName.name does not use the variable name, is using the literal word name to insert the word “name” as a dictionary variable
print(i) -- results in out put statement (13:55:34.626 name - Server)
end
end
Players.PlayerAdded:Connect(dictionaryName)
-- end first part

-- second part
-- second part takes the errored dictionary value the word “name” is used instead of player’s name, but the code does change the value of the key from the dictionary. I will need to know how to use variable to look up dictionary variable (player name) to change there individual value.

game.ReplicatedStorage.HideTest1.OnServerEvent:Connect(function(player)
local name = player.Name -- does this work? yes
if dictionaryName[name] == false then
dictionaryName[name] = true
else
dictionaryName[name] = false
end
end)
– end second part -- If you use dictionaryName.name anywhere else, just change it to dictionaryName[name]
1 Like

yes! i tried this before i did the pairs loop. but i also did not try this when searching the value of the key. that is why in my code (where there is more print functions) did not appear to change. thanks for the help. let me check if this works for the 3rd part too.

yes this works for part 3 too! thank you both for your help. Will remember this!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.