Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Making data in a dictionary correspond?

I've set up this dictionary (I think), where the number is the PlayerLevel, the first string is the PlayerStatus and the second string is the ImageURL...

local dictionary = {
	[1] = {"N O V I C E", "www.url1here"},
	[2] = {"E X P E R I E N C E D", "www.url2here"},
	[3] = {"P R O", "www.url3here"},
}

The player has a leaderstats named PlayerLevel, how would I set up two more variables like so:

local PlayerLevel = Player.leaderstats.PlayerLevel.Value
local PlayerStatus = --?
local ImageURL = --?

The two other variables must correspond to its level.

1 answer

✓ Accepted answer

Good question!

First, I think it's important to point something out.

local tb = { 
	"hello ",
	"there"
} -- this is the same as:

local tb = {
	[1] = "hello ",
	[2] = "there"
}

print(tb[1]..tb[2])

Lua will auto-index things; you don't have to manually put the numbers.


Answer

In your code that I changed to make it shorter by removing the indexes, to get the values from the table you do this:

local dictionary = {
    {"N O V I C E", "www.url1here"},
    {"E X P E R I E N C E D", "www.url2here"},
    {"P R O", "www.url3here"},
}

local PlayerLevel = Player.leaderstats.PlayerLevel.Value
local PlayerStatus = dictionary[PlayerLevel][1]
local ImageURL = dictionary[PlayerLevel][2]
This isn't actually a dictionary.

I hope that helps you!

5 comments

0

Check when the player levels up and change the variables corresponding to the new level for this to work properly :D

0

It throws this error where the PlayerStatus variable is: attempt to index field '?' (a nil value)

0

That means PlayerLevel is nil. Define it properly.

0

It's working in a Server but not in Studio Test mode, any ideas?

0

Yeah, use WaitForChild. Example: PlayerLevel = Player:WaitForChild("leaderstats"):WaitForChild("PlayerLevel").Value

This discussion is preserved as it was. New answers, comments, and votes are closed.