Data Store not working

I’ve made a data store script that is supposed to save 1 value ( inside Replicated Storage.

It prints not saved and doesn’t save a color3 value inside a folder and I’ve been trying for hours.


local DataStoreService = game:GetService("DataStoreService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AmbientData = DataStoreService:GetDataStore("AmbientData")


PlayersService.PlayerAdded:Connect(function(Player)
    
    local PlayerDataFolder = ReplicatedStorage:WaitForChild(Player.Name)
    
    print(PlayerDataFolder)
    
    local Data
    
    local success, errormessage = pcall(function()
    
         Data = AmbientData:GetAsync(Player.UserId .. "-AmbientData")

    end)    
    
    if success then
         PlayerDataFolder.Data.Ambient.Value = Color3.fromRGB(Data)
         print(Color3.fromRGB(Data))
    else
        
        print("no ")
    end
        
end)

PlayersService.PlayerRemoving:Connect(function(Player)
    
    local PlayerDataFolder = ReplicatedStorage:WaitForChild(Player.Name)
    
    local success, errormessage = pcall(function()
        AmbientData:SetAsync(Player.UserId .. "-AmbientData", PlayerDataFolder.Data.Ambient.Value)
    end)
    
    if success then
        print("saved")
    else
        print("not saved")
    end
    
end)```

Have you published the game and enable the API access?
Also DataStore can’t store color3 values only String and int

Why is this question continuously asked? If he doesn’t get the output message “Please enable API services” then he obviously has it enabled

Than it is the fact that it can’t store color3 values

does the first print works ? Problem is prob as said before, you can’t store a color3 value. If you want to store the color 3, you would need to store a table with the RGB of the color.

You can’t store Tables either. And i know it can’t because i tried before and it didn’t work

umh you can, the data in my game is saved as a big table, you are probably doing something wrong when setting the data or getting it :confused:

read : How do you save Tables to datastore? - #4 by NicholasY4815

oh wait a second i just remembered something i mixed up dictionaries and tabels oops sorry

You are not able to save Color3 values inside of datastores since datastores only accept UTF-8 characters. You would have to come up with a way to save the values separately and put them together when loading data. Maybe try using a table with R,G,B set to an integer.

Something on the lines of this:

local Data = {
     ["Ambient"] = {
         ["R"] = 215, 
         ["G"] = 215, 
         ["B"] = 215,  
     }
}

-- get data, then combine values
local GotData = DATASTORE:GetAsync() -- get async call

-- combine integers to make color
Object = Color3.fromRGB(GotData["Ambient"]["R"],GotData["Ambient"]["G"],GotData["Ambient"]["B"])

that is not a Table that is a Dictionary which cannot be saved in DataStore

Tables and dictionaries are considered the same thing in Lua.

type{key = 'value'} --> table
type{1, 2, 3} --> table

Then how come that is can’t save a dictionary but you can save a table

That would depend on how you are scripting your data saving and how your data table looks.

This isn’t Python where arrays and dictionaries are two different types. You can save a dictionary just as you can save an array.
You just can’t save a hybrid of arrays and dictionaries.

1 Like

What do you mean? That’s a bit false.

A table and a dictionary are the same things the only difference you can make is of indexes between arrays and dictionaries in Lua, both still considered tables which you can save easily in Roblox.

Only strings can be saved to what DataStoreService interfaces with (tables internally being JSONEncoded).

Edit: Rather than waiting for replies, you could just test out a theory yourself instantaneously getting the results.

2 Likes

If it’s a Color3 Value you’re trying to save, you could serialize it

local color = Color3.fromRGB(100,210,244) 
--> equivalent to Color3.new(100/255, 210/255, 244/255)
-- printing color.R would output the value color.R/ 255, since the value returned would've been scaled to a maximum of 1 (representing 255)

local function toSaveable(color)
   return {R = color.R * 255, G = color.G * 255, B = color.B * 255}
end
 
-- save
DataStore:SetAsync(key, toSaveable(color))

-- retrieve
local color = DataStore:GetAsync(key)
color = Color3.fromRGB(color.R, color.G, color.B) 
 
-- or just have the function return the same color.R, color.G, color.B values and construct using Color3.new(color.R, color.G, color.B) instead of multiplying and constructing using Color3.fromRGB