Datastore won't work?

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.

2 Likes

Found the error:

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)

Hope this works.

1 Like

What’s the difference? Roblox uses luau, so there are advanced operators. Also, that is just testing, it has nothing to do with datastore.

What you mean with difference, in luau there’s not any “+=” operator.

Have you tried it out for yourself? Look at this documentation: Operators | Documentation - Roblox Creator Hub, and as I said, it has nothing to do with the datastore.

Yeah, look at this table:
image

Maybe it’s just not documented, and once again, have you tried again, and twice again, that has nothing to do with the datastore.

As I told before, there’s not any “+=” operator in luau.
image

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.

Well, I get it
About the datastore error:

		Money.Value = Data["Money"].Value
		Fans.Value = Data["Fans"].Value
		Color.Value = Data["Color"].Value
		Material.Value = Data["Material"].Value

You can’t do that, because Data[] is a table and not an IntValue

Yes, but I am doing Data, which is a table, filled with items, and then the item inside of it which is an int value, and then the value of it.

This is the way you have to do that, because a table has not a .Value property

1 Like

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.

1 Like

There’s an issue. Now it says

  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()
1 Like

there is a += operator, you are just doing it wrong
this script should really be something like this:

local value = 1
value += 2
print(value)
2 Likes

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.

do you have any errors in the output?

1 Like

yes there is? I use it all the time

Can’t find the line where are you calling for the function:

MeshPart(v, Material, Color)

Maybe the error is there, cuz the error says color can’t be nil

Yeah, didn’t know about it. Tysm.

1 Like