so I have this script where, everytime a player joins the game their name and bodycolor gets added into a dictionary within a table:
_G.playerinformation = {}
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Wait()
for _, v in pairs(player.Character:GetChildren()) do
if v:IsA("BasePart") then
local mainColor = v.Color
local r, g, b = mainColor.r, mainColor.g, mainColor.b
local str = string.format("%0.f, %0.f, %0.f", r*255, g*255, b*255)
local RGBValues = Color3.new(table.unpack(string.split(str, ", ")))
_G.playerinformation[player.Name] = RGBValues
print(_G.playerinformation)
break
end
end
end)
This works fine however, how could I go about accessing a specific player RGBValue and changing it?
Like for example, when a user clicks a button it will iterate through this table, match the player.Name who clicked the button for the playerName inside the table, and grab the rgb value and change it which will change their bodycolor.
To access it: local color = _G.playerinformation[playerName] -- will give you the Color3
To change it: _G.playerinformation[playerName] = Color3.new() -- setting it to an empty Color3 (black)
To change the color of their character, simply iterate over their character instance and set the color:
for _, instance in pairs(player.character:GetChildren()) do
if instance:IsA("BasePart") then
instance.Color = _G.playerinformation[player.Name]
end
end
Side note: your current code is doing all sorts of weird stuff that I’m not sure you’re intending to do.
Firstly, it’s only storing the color of the first BasePart it encounters within the player and then it breaks the loop entirely. This is problematic for a number of reasons (i.e., picking the color of the transparent HumanoidRootPart). Instead of this, you should use the characters Body Colors instance (assuming these are regular characters).
Second, you’re formatting, packing, and unpacking the color for presumably no reason. The majority of this code could be simplified to: _G.playerinformation[player.Name] = v.Color
as you are not modifying the color in any way.
Using _G is probably the least of your concerns at this point.