Tables not working; team change

One of the local scripts:

local GamePassService = game:GetService('MarketplaceService')
local player = game.Players.LocalPlayer 

script.Parent.MouseButton1Down:connect(function()
	
	if player:IsInGroup(5770388) or GamePassService:UserOwnsGamePassAsync(player.userId, 4702761) then
		
		game.ReplicatedStorage.GUI.Assign:FireServer(BrickColor.new("Mint"))
		
	end
	
end)

Server:

local lookup = {
	[BrickColor.new("Mint")] = {group = 5770388, gamepass = 4702761};
	[BrickColor.new("New Yeller")] = {group = 4462366, gamepass = 8104692};
	[BrickColor.new("Wheat")] = {group = 8349787, gamepass = 4702761};
	[BrickColor.new("Crimson")] = {group = 5336968, gamepass = 0};
	[BrickColor.new("Really blue")] = {group = 5336914, gamepass = 7913465};
	[BrickColor.new("Forest green")] = {group = 4219097, gamepass = 0};
	[BrickColor.new("Burlap")] = {group = 5336904, gamepass = 6086659};
	[BrickColor.new("Moss")] = {group = 8310499, gamepass = 0};
	[BrickColor.new("Brown")] = {group = 5351323, gamepass = 0};
	[BrickColor.new("Dark stone grey")] = {group = 5336916, gamepass = 12626946};
	[BrickColor.new("White")] = {group = 5351327, gamepass = 12626936};
	[BrickColor.new("Black")] = {group = 5754090, gamepass = 0};
	[BrickColor.new("Really red")] = {group = 5288092, gamepass = 12626928};
	[BrickColor.new("Electric blue")] = {group = 4419482, gamepass = 4708293};
}

local GamePassService = game:GetService('MarketplaceService')

game.ReplicatedStorage.GUI.Assign.OnServerEvent:connect(function(plr, color)
	print (lookup[color])
	
	local info = lookup[color]
	print (info)
	if info then
		if plr:IsInGroup(info.group) or GamePassService:UserOwnsGamePassAsync(plr.UserId, info.gamepass) then
			plr.TeamColor = color
		end
	end
end)

The prints I placed show this:

I don’t think color values work like that. Explained by @Blockzez. Have you tried using strings instead and adding the BrickColor value to the elements of the table?

Like:
["Mint"] = {group = 5770388, gamepass = 4702761, color = BrickColor.new("Mint")};

I was just thinking of something along those lines. Now to edit 1000 local scripts haha, only got about 24 minutes to do all of this before I got to go IRL as well; oh boy.

Fixed with the following:

local lookup = {
	["Mint"] = {group = 5770388, gamepass = 4702761},
	["New Yeller"] = {group = 4462366, gamepass = 8104692};
	["Wheat"] = {group = 8349787, gamepass = 4702761};
	["Crimson"] = {group = 5336968, gamepass = 0};
	["Really blue"] = {group = 5336914, gamepass = 7913465};
	["Forest green"] = {group = 4219097, gamepass = 0};
	["Burlap"] = {group = 5336904, gamepass = 6086659};
	["Moss"] = {group = 8310499, gamepass = 0};
	["Brown"] = {group = 5351323, gamepass = 0};
	["Dark stone grey"] = {group = 5336916, gamepass = 12626946};
	["White"] = {group = 5351327, gamepass = 12626936};
	["Black"] = {group = 5754090, gamepass = 0};
	["Really red"] = {group = 5288092, gamepass = 12626928};
	["Electric blue"] = {group = 4419482, gamepass = 4708293};
}

local GamePassService = game:GetService('MarketplaceService')

game.ReplicatedStorage.GUI.Assign.OnServerEvent:connect(function(plr, color)
	print (lookup[color])
	
	local info = lookup[color]
	print (info)
	if info then
		if plr:IsInGroup(info.group) or GamePassService:UserOwnsGamePassAsync(plr.UserId, info.gamepass) then
			plr.TeamColor = BrickColor.new(color)
		end
	end
end)
1 Like

Can you elaborate by how it doesn’t work like that? Anything but nil and NaN works for table indexes.

Oh, I didn’t know that. Why didn’t it work before though? Oops, I think it was unrelated. Sorry, I’m a bit dumb at the moment :sweat_smile:

table indexes only gets the index if they rawequal. As color values in Roblox Lua are objects, rawequal compare by them objects. Brick colors have the __eq metamethod so despite thatBrickColor.new("Mint") == BrickColor.new("Mint") is true, rawequal(BrickColor.new("Mint"), BrickColor.new("Mint")) is false so they don’t get the index of that value.

local color1 = BrickColor.new("Mint")
local color2 = BrickColor.new("Mint")
local t = { [color1] = "Mint" }
print(t[color1]) --> Mint
print(t[color2]) --> nil
print(color1 == color2) -- true
print(rawequal(color1, color2)) --> false
print(rawequal(color1, color1)) --> true

The problem OP is that the OP creates a table with different brick color object than the remote event sent despite them logically being the same. You’re correct that strings works.

1 Like

Ah, that makes sense. Thanks for the info.