Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to save and load Color3Values to Datastore using JSON?

Asked by 6 years ago

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

Log in to vote
0
Answered by
Hexcede 52
6 years ago

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}
0
Thats not what im asking for. I'm asking how i would save those values to datastore. DeathGunner132 120 — 6y
0
Look on the wiki... You don't need to make a pointless request on Scripting Helpers. Read the guidelines please and thank you. Hexcede 52 — 6y
Ad
Log in to vote
0
Answered by
oftenz 367 Moderation Voter
6 years ago
Edited 6 years ago

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.

0
You cant save an array to datastore.. DeathGunner132 120 — 6y
0
you can, and you can also save Color3's. Roblox's DataStoreService can save any datatype except instances spr_ead 47 — 6y
0
Then how would I load it? DeathGunner132 120 — 6y
0
You are loading it when the player joins. line 11 local newdata = RGBdata:GetAsync(player) oftenz 367 — 6y
Log in to vote
0
Answered by
spr_ead 47
6 years ago
Edited 6 years ago

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)
Log in to vote
0
Answered by 6 years ago

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!

Answer this question