Hello, I need to make a datastore for my game. There are 4 values in the datastore, being: Money, Fans, Color, and Material. However, it won’t work. Here is my code:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
Players.PlayerAdded:Connect(function(Player)
-- Statistics
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue", Leaderstats)
Money.Name = "Money"
local Fans = Instance.new("IntValue", Leaderstats)
Fans.Name = "Fans"
-- Multipliers/Cosmetics
local Color = Instance.new("Color3Value", Player)
Color.Name = "Color"
local Material = Instance.new("StringValue", Player)
Material.Name = "Material"
local Data = PlayerData:GetAsync(Player.UserId)
if Data ~= nil then
Money.Value = Data["Money"].Value
Fans.Value = Data["Fans"].Value
Color.Value = Data["Color"].Value
Material.Value = Data["Material"].Value
else
Money.Value = 0
Fans.Value = 0
Color.Value = Color3.new(1, 1, 1)
Material.Value = "CorrodedMetal"
end
Money.Value += 5
end)
Players.PlayerRemoving:Connect(function(Player)
local PlayerDataTable = table.create(0, Player)
for i, v in pairs(Player.leaderstats:GetChildren()) do
table.insert(PlayerDataTable, v)
end
PlayerData:SetAsync(Player.UserId, PlayerDataTable)
end)
If it doesn’t work, money should be 5, and if it does it should be 5+5 or money+5. If anybody knows why it’s not working please let me know. Thanks in advance.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
Players.PlayerAdded:Connect(function(Player)
-- Statistics
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue", Leaderstats)
Money.Name = "Money"
local Fans = Instance.new("IntValue", Leaderstats)
Fans.Name = "Fans"
-- Multipliers/Cosmetics
local Color = Instance.new("Color3Value", Player)
Color.Name = "Color"
local Material = Instance.new("StringValue", Player)
Material.Name = "Material"
local Data = PlayerData:GetAsync(Player.UserId)
if Data ~= nil then
Money.Value = Data["Money"]
Fans.Value = Data["Fans"]
Color.Value = Data["Color"]
Material.Value = Data["Material"]
else
Money.Value = 0
Fans.Value = 0
Color.Value = Color3.new(1, 1, 1)
Material.Value = "CorrodedMetal"
end
Money.Value += 5
end)
Players.PlayerRemoving:Connect(function(Player)
local PlayerDataTable = table.create(0, Player)
for i, v in pairs(Player.leaderstats:GetChildren()) do
table.insert(PlayerDataTable, v)
end
PlayerData:SetAsync(Player.UserId, PlayerDataTable)
end)
So you trying to access into an IntValue with the “.Value” while you are getting the data from a table, also found another error in lua you can’t do this:
Money.Value += 5
Instead you have to:
Money.Value = Money.Value + 5
Like this:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
Players.PlayerAdded:Connect(function(Player)
-- Statistics
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue", Leaderstats)
Money.Name = "Money"
local Fans = Instance.new("IntValue", Leaderstats)
Fans.Name = "Fans"
-- Multipliers/Cosmetics
local Color = Instance.new("Color3Value", Player)
Color.Name = "Color"
local Material = Instance.new("StringValue", Player)
Material.Name = "Material"
local Data = PlayerData:GetAsync(Player.UserId)
if Data ~= nil then
Money.Value = Data["Money"]
Fans.Value = Data["Fans"]
Color.Value = Data["Color"]
Material.Value = Data["Material"]
else
Money.Value = 0
Fans.Value = 0
Color.Value = Color3.new(1, 1, 1)
Material.Value = "CorrodedMetal"
end
Money.Value = Money.Value + 5
end)
Players.PlayerRemoving:Connect(function(Player)
local PlayerDataTable = table.create(0, Player)
for i, v in pairs(Player.leaderstats:GetChildren()) do
table.insert(PlayerDataTable, v)
end
PlayerData:SetAsync(Player.UserId, PlayerDataTable)
end)
Well, it works for me as I have tested ingame, and thrice again, it has nothing to do with the datastore. It doesn’t even break the script, and it works.
I am doing Data[] and then a stat in it, and then the value of it. If I did Data.Value you’d be right, but I am doing Data[Item].Value. Could you explain why the intvalue inside of a table will not work?
You can’t save an IntValue object into data store, instead of this you have to save the value of the IntValue, let me show an example:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Example")
local folder = workspace.Example
local function createTableFromFolder(folder)
if not folder then
warn("Folder does not exist.")
return
end
local newTable = {}
for _, object in pairs(folder:GetChildren()) do
if object:IsA("IntValue") then
newTable[object.Name] = object.Value
end
end
return newTable
end
local function example()
local data = createTableFromFolder(folder)
local success, message = pcall(function()
data = DataStore:SetAsync(data)
end)
if not success then
warn("There was an error with saving player data.")
end
end
This code finds every IntValue from a folder and then save its values into a table.
15:11:35.029 Unable to assign property Value. Color3 expected, got nil - Server - Stats:26
What should I do about this? I also have a script called LocalMap that might affect it called LocalMap, this is it:
local Players = game:GetService("Players")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Statue = workspace.Statue.Char
local Players = game:GetService("Players")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Statue = workspace.Statue.Char -- Assuming "Statue" is a Model in the workspace
-- Player Item Functions
function Accessory(v, Material, Color) -- Accessory Function
local Handle = v:FindFirstChildWhichIsA("MeshPart")
Handle.TextureID = ""
Handle.Material = Material
Handle.Color = Color
end
function MeshPart(v, Material, Color) -- Part Function
v.Material = Material
v.Color = Color
end
function BaseAppearance(Morph, MorphHumanoid, Scale, Material, Color)
MorphHumanoid:WaitForChild("BodyHeightScale").Value *= Scale
MorphHumanoid:WaitForChild("BodyDepthScale").Value *= Scale
MorphHumanoid:WaitForChild("BodyWidthScale").Value *= Scale
MorphHumanoid:WaitForChild("HeadScale").Value *= Scale
for i, v in pairs(Morph:GetChildren()) do
if v:IsA("Pants") then
v.PantsTemplate = ""
elseif v:IsA("Shirt") then
v.ShirtTemplate = ""
elseif v:IsA("ShirtGraphic") then
v:Destroy()
elseif v:IsA("Accessory") then
Accessory(v, Material, Color)
elseif v:IsA("MeshPart") then
MeshPart(v, Material, Color)
end
end
MorphHumanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
end
function StatueAppearance()
-- Variables
Character.Archivable = true
local CharacterMorph = Character:Clone()
local MorphHumanoid = CharacterMorph:WaitForChild("Humanoid")
-- Statue Body Appearance Variables
local Player_Material = Player:WaitForChild("Material").Value
local Scale = 15
local Material = Enum.Material[Player_Material]
local Color = Player:WaitForChild("Color").Value
-- Statue Position
CharacterMorph:SetPrimaryPartCFrame(Statue.PrimaryPart.CFrame) -- Set the character's position to match the Statue's position
-- Revert the statue to normal and set all the changes
Statue:ClearAllChildren()
CharacterMorph.Parent = Statue
BaseAppearance(CharacterMorph, MorphHumanoid, Scale, Material, Color)
Character.Archivable = false
end
StatueAppearance()
roblox uses JSONEncode which means that you cannot save color3 values with datastores, if you want to save a color3 value you should encode it into a table like so
local color = Color3.fromRBG(255,125,50)
local colortbl = {
r = color.R,
b = color.B
g = color.G
}
and then of course decode it when you want to use it.