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

How to save and load Color3Values to Datastore using JSON?

I've asked many questions regarding this issue and havent gotten a valid answer yet. I know how to save and load with Datastore but don't know how you would save Color3 Values using JSON.

4 answers

You can't technically "save a Color3" but you can save the RGB values. According to the Roblox wiki Color3s have three values... R G and B. R is red, G is green, B is blue.

To recreate a Color3 from it's red, green, and blue colors you can use Color3.fromRGB or Color3.new and divide your RGB values by 255 to get a decimal.

Wiki page: http://wiki.roblox.com/index.php?title=API:Color3

Example table to save:

{R=color3.R, G=color.G, B=color3.B}

2 comments

0

Thats not what im asking for. I'm asking how i would save those values to datastore.

0

Look on the wiki... You don't need to make a pointless request on Scripting Helpers. Read the guidelines please and thank you.

You can save the RGB value simply with a datastore - I will reference this wiki throughout my explanation.

--In a script in ServerScriptService. Data stores are SERVER ONLY!

local DataStoreService = game:GetService("DataStoreService")
local RGBdata= DataStoreService:GetDataStore("RGB")
local a = 5   -- Insert first RGB color
local b = 10   -- Insert second RGB color
local c = 50   -- Insert third RGB color
local savetable = {a, b, c}

local function onPlayerAdded(player)
	local newdata = RGBdata:GetAsync(player)
	--This is your RGB that you saved in the data store.
end
 
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:connect(function(player)
	RGBData:SetAsync(player, savetable)
end)

What you are doing, is when the player joins your game you simply get a sync from the data store. When the player leaves you do the opposite. Let me know if you have any additional questions.

4 comments

0

You cant save an array to datastore..

0

you can, and you can also save Color3's. Roblox's DataStoreService can save any datatype except instances

0

Then how would I load it?

0

You are loading it when the player joins. line 11 local newdata = RGBdata:GetAsync(player)

You can use the functions HttpService:JSONEncode when saving the data, and HttpService:JSONDecode when retrieving.

local dss = game:GetService('DataStoreServive'):GetDataStore('Example')
local http = game:FindService('HttpService')

function save(key, value)
       value = http:JSONEncode(value)
       local success, message = pcall(function()
               dss:SetAsync(key, value)
       end)
      return success, message
end

function retrieve(key)
       return http:JSONDecode(dss:GetAsync(key)) or nil
end

-- Simple usage

game.Players.ChildAdded:Connect(function(player)
       local data = retrieve('user_'..player.UserId)
       print(data)
end)

game.Players.ChildRemoved:Connect(function(player)
       local s, m = save('user_'..player.UserId, Color3.new(255/255,0,0))
       if s then
              print('Success')
       else
              print('Failure: '..m)
       end
end)

I found this youtube video helpful. At the beginning it is a bit slow and confusing but once you get through the video you should be able to do what you are trying to do. Here is the link. Hope it helps!

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