I’m trying to save Hair Color in a Color3Value in a Data Store. I was not familiar with Data Stores so I was going based on a template Data Store script I found. (Script works fine without the Color3Values) I would like to take this opportunity to learn more about Data Stores so please give as much explanation as to what I could have done better and how I can fix this issue.
The issue happens with the Player leaves the game (when the data is saved). The error it gives me is
Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters. I don’t get this error when I have the Color3Value removed from the script.
Thanks for the help!
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData_BETATEST")
local function onPlayerJoin(player) -- Runs when players join
local PlayerData = Instance.new("Folder") --Sets up leaderstats folder
PlayerData.Name = "PlayerData"
PlayerData.Parent = player
local HasCreated = Instance.new("BoolValue") --Sets up value for leaderstats
HasCreated.Name = "HasCreated"
HasCreated.Parent = PlayerData
local Outfit = Instance.new("IntValue")
Outfit.Name = "Outfit"
Outfit.Parent = PlayerData
local Hair = Instance.new("IntValue")
Hair.Name = "Hair"
Hair.Parent = PlayerData
local HairColor= Instance.new("Color3Value")
HairColor.Name = "HairColor"
HairColor.Parent = PlayerData
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
HasCreated.Value = data['HasCreated']
Outfit.Value = data['Outfit']
Hair.Value = data['Hair']
HairColor.Value = data['HairColor']
else
HasCreated.Value = false
Outfit.Value = 1
Hair.Value = 1
HairColor.Value = Color3.fromHSV(0,0,1)
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.PlayerData:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end
--local success, err = pcall(function()
-- if not success then
-- warn('Could not save data!')
-- end
--end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)