I get abnormal numbers for Color3.R, Color3.G, and Color3.B

Im trying to make it so the player can change his bodycolor and it saves when he joins.
When I print the the R,G,B values of color3, The values are abnormal
image

This is how im storing it:

local DataStore = game:GetService("DataStoreService")
local Data = DataStore:GetDataStore("PlayerColor")


game.ReplicatedStorage.LoadColors.OnServerEvent:Connect(function(ID, R,G,B)
	local Table = {R,G,B}
	print(Table)
	Data:SetAsync(ID, Table)
end)

This is how I load it:

local success, Color = pcall(function()
	return Data:GetAsync(PlrID) or {248,248,248}
	
end)
if success then
	local R = Color[1]
	print(R)
	local G = Color[2]
	print(G)
	local B = Color[3]
	print(B)

Is there any fix to this?

3 Likes

To get the original number times the number by 255

1 Like

Color3 describes colours using the range of 0-1 and I’m assuming you’re expecting it to be in the range 0-255.

To fix it all you have to do is times each number by 255, and then round it.

e.g.

local R = math.min(Color[1] * 255)

Although do you necessarily need to do this if you’re just going to apply the colour back again?
Can’t you just put it back to a Color3 using

Color3.new(table.unpack(Color))
1 Like

When I multiply it by 255 (all values) I turn completly black for some reason.
When I Use Color3.new(table.unpack(Color)) I turn completly blue. But when i do Color3.FromRBG(table.unpack(Color)) I turn red even when I will save it as white.

2 Likes

Does this line print the correct numbers that you expect?

1 Like

When i save the color as bright red (255,0,0) i get this

                [1] = 441168050,
                [2] = 1,
                [3] = 0

When i print it as white. i get this.

                [1] = 441168050,
                [2] = 0.9725490212440491,
                [3] = 0.9725490212440491
2 Likes

And that is from the print statement i highlighted? then the problem is with your input to the function.

Maybe you should try to use Hex to simplify and only have to save one string instead:

2 Likes

Bright red as a Color3 would be (1,0,0), not (255,0,0)

I tried using hex but when I tried converting a hex to a rbg it seems really complex.

local red = Color3.fromRGB(255, 0, 0)
local magenta = Color3.fromRGB(236, 0, 140)

local redHex = red:ToHex()
print(redHex)  --> ff0000

local magentaHex = magenta:ToHex()
print(magentaHex)  --> ec008c

to get it back use FromHex:

red = Color3.fromHex("ff0000")
--or using the variables above:
red = Color3.fromHex(redHex)
2 Likes

If you still want to use the RGB (255) values then you’ll have to show us your script where you call the function to save it to see what you’re doing wrong there.

The problem is coming from the script that fires the server event that you have shown us.

1 Like

Heres a script that might help. Its realated but all it does is get the R,G,B

local BodyColor = game.ReplicatedStorage.CharacterColors.Red["Body Colors"].HeadColor3 --Gets the red HeadColor3
local PlrID = game.Players.LocalPlayer.UserId
local Char = game.Players.LocalPlayer.Character
local NewBodyColor = {BodyColor.R,BodyColor.B,BodyColor.G}
local ColorSaver = game.ReplicatedStorage.SaveColors
local ColorLoader = game.ReplicatedStorage.LoadColors


script.Parent.MouseButton1Click:Connect(function()
	ColorSaver:FireServer(PlrID, BodyColor.R, BodyColor.G, BodyColor.B)
	wait(0.1)
	ColorLoader:FireServer(PlrID, BodyColor.R, BodyColor.G, BodyColor.B)
end)

1 Like

These values should be between 0 and 1, you should print these out and see if they are being set improperly or if they are between 0 and 1.

(I could be wrong you’d have to test this, I’m not on studio right now)

well i tested it:

So if you’re not getting numbers that look like that then you’re setting the color wrong somewhere else.

1 Like

I get R1 G0 B0
The table is wrong then because the table saids that these are the numbers:

                [1] = 441168050,
                [2] = 1,
                [3] = 0
1 Like

Well you are sending those numbers with the fireserver, so I guess you need to add more print statements to see where it is not doing what you think it is doing.

print all of this stuff:

1 Like

The prints are working find. But I put prints in the script that saves the color. I get Insanly big numbers:

R: 112497852750
G: 112497852750
B: 112497852750

1 Like

They all print 1,0,0 ?

Well I’m out of ideas I guess.

1 Like

When using a RemoteEvent the first paramater is ALWAYS the Player object that fired the event.

So where you have

game.ReplicatedStorage.LoadColors.OnServerEvent:Connect(function(ID, R,G,B)

When this RemoteEvent is called the “ID” is actually the Player and then R becomes the ID that you sent from the client.

So replace that with

game.ReplicatedStorage.LoadColors.OnServerEvent:Connect(function(player, ID, R,G,B)

and it should work fine.

Although I wouldn’t reccomend manually sending the ID since then exploiters could replace that with anyones ID and mess up someone elses data. You can just get the ID from the player object that is sent with Player.UserId and remove the ID parameter entirely.

3 Likes

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