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:

1{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.

01--In a script in ServerScriptService. Data stores are SERVER ONLY!
02 
03local DataStoreService = game:GetService("DataStoreService")
04local RGBdata= DataStoreService:GetDataStore("RGB")
05local a = 5   -- Insert first RGB color
06local b = 10   -- Insert second RGB color
07local c = 50   -- Insert third RGB color
08local savetable = {a, b, c}
09 
10local function onPlayerAdded(player)
11    local newdata = RGBdata:GetAsync(player)
12    --This is your RGB that you saved in the data store.
13end
14 
15game.Players.PlayerAdded:Connect(onPlayerAdded)
16game.Players.PlayerRemoving:connect(function(player)
17    RGBData:SetAsync(player, savetable)
18end)

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.

01local dss = game:GetService('DataStoreServive'):GetDataStore('Example')
02local http = game:FindService('HttpService')
03 
04function save(key, value)
05       value = http:JSONEncode(value)
06       local success, message = pcall(function()
07               dss:SetAsync(key, value)
08       end)
09      return success, message
10end
11 
12function retrieve(key)
13       return http:JSONDecode(dss:GetAsync(key)) or nil
14end
15 
View all 30 lines...
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