Find item of table in table

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to color the players according to my datastore

  2. What is the issue? Include screenshots / videos if possible!
    idk how to find an item in a table

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

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

my code:

local Players = game.Players
local DatastoreService = game:GetService("DataStoreService")

local database = DatastoreService:GetDataStore("Test")
local sessionData = {}

Players.PlayerAdded:Connect(function(player)
	
	
	local success = nil
	local playerData = nil
	local attempt = 1
	
	repeat
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)
		warn(playerData)

		attempt += 1
		if not success then
			warn(playerData)
			wait(3)
		end
	until success or attempt == 5
	
	if success then
		print("Connected to database")
		if playerData then
			print("Assigning default data")
			playerData = {
				["ClablocksVisor"] = true,
				["LeftLegColor"] = 3,
				["RightLegColor"] = 3,
				["LeftArmColor"] = 1,
				["RightArmColor"] = 1,
				["TorsoColor"] = 2,
				["HeadColor"] = 1
			}
		end
		sessionData[player.UserId] = playerData
	else
		warn("Unable to get data for", player.UserId)
		player:Kick("Failed to find data. Rejoin")
	end
	
	
	local Colors = {["1"] = BrickColor.new("White"), ["2"] = BrickColor.new("Grey"), ["3"] = BrickColor.new("Black"), ["4"] = BrickColor.new("Really red"), ["5"] = BrickColor.new("Neon orange"), ["6"] = BrickColor.new("New Yeller"), ["7"] = BrickColor.new("Lime green"), ["8"] = BrickColor.new("Teal"), ["9"] = BrickColor.new("Really blue"), ["10"] = BrickColor.new("Pink"), ["11"] = BrickColor.new("Royal purple"), ["12"] = BrickColor.new("Brown"), } --line for the colors
	
	for i, piece in pairs(player.Character:GetChildren()) do --the wanted loop
		if piece.Name == "Left Leg" then
			print(Colors.playerData.LeftLegColor) --here is the error
		end
	end
	
end)

function PlayerLeaving(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		
		repeat
			success, errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId])
			end)
			
			attempt += 1
			if not success then
				warn(errorMsg)
				wait(3)
			end
		until success or attempt == 5
		
		if success then
			print("Data saved for", player.Name)
		else
			warn("Unable to save for", player.Name)
		end
		
	end
end
Players.PlayerRemoving:Connect(PlayerLeaving)

function ServerShutdown()
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)
		end)
	end
end

game:BindToClose(ServerShutdown)

You never added a property called playerData to the Colors table

Did you mean to just write playerData.LeftLegColor?

what i wanted was to get the set color the player has for their left leg. so it would normally be set to probably 3 i think (black) and would print it, im working on setting the color.

d_real, do you have an answer? but if you dont thats ok.

You do not need to provide keys unless you want dictionary behavior. I strongly recommend grasping the concept of tables before moving on.

local Colors = {["1"] = BrickColor.new("White"), ["2"] = BrickColor.new("Grey"), ...

Should be replaced with

local Colors = {BrickColor.new("White"), BrickColor.new("Grey"), ...

as we do not need the keys. Now we can reference items from the table using their indexes. For example "White" would be index 1 so we reference it using

Colors[1]

Knowing this, we can get the position you want to index from playerData and grab the corresponding value from the Colors table.

Colors[playerData.LeftLegColor]

I hope this helps.

1 Like

thank you so much! :smiley: it works!

1 Like

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