Datastore not working

I am trying to make a simple Datastore system that saves multiple NumberValues in a Data folder under the player, I am getting no errors and cannot figure out the issue.

local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Data = Instance.new("Folder", player)
	Data.Name = "Data"

	local Level = Instance.new("NumberValue", Data)
	Level.Name = "Level"
	Level.Value = 1

	local Leaves = Instance.new("NumberValue", Data)
	Leaves.Name = "Leaves"
	Leaves.Value = 0

	local LPS = Instance.new("NumberValue", Data)
	LPS.Name = "LPS"
	LPS.Value = 0

	local Fertilizer = Instance.new("NumberValue", Data)
	Fertilizer.Name = "Fertilizer"
	Fertilizer.Value = 0

	local Soil = Instance.new("NumberValue", Data)
	Soil.Name = "Soil"
	Soil.Value = 0

	local Wood = Instance.new("NumberValue", Data)
	Wood.Name = "Wood"
	Wood.Value = 0

	local Water = Instance.new("NumberValue", Data)
	Water.Name = "Water"
	Water.Value = 0

	local success, playerData = pcall(function()
		return playerDataStore:GetAsync(player.UserId)
	end)

	if success and playerData then
		Level.Value = playerData.Level or Level.Value
		Leaves.Value = playerData.Leaves or Leaves.Value
		LPS.Value = playerData.LPS or LPS.Value
		Fertilizer.Value = playerData.Fertilizer or Fertilizer.Value
		Soil.Value = playerData.Soil or Soil.Value
		Wood.Value = playerData.Wood or Wood.Value
		Water.Value = playerData.Water or Water.Value
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Data = player:FindFirstChild("Data")
	if Data then
		local playerData = {
			Level = Data.Level.Value,
			Leaves = Data.Leaves.Value,
			LPS = Data.LPS.Value,
			Fertilizer = Data.Fertilizer.Value,
			Soil = Data.Soil.Value,
			Wood = Data.Wood.Value,
			Water = Data.Water.Value
		}

		local success, errorMessage = pcall(function()
			playerDataStore:SetAsync(player.UserId, playerData)
		end)

		if not success then
			warn("Error")
		end
	end
end)

1 Like

There are 2 possible issues.

  1. You are not using a bind to close. You can learn more about them here:
  1. You are not using JSON encode and decode for saving data.

JSON endcode:

JSON decode:

I would suggest trying to implement JSON methods and bind to closes as these will probably fix your issue. :grin:

1 Like

How are you changing the values while the game is running? It’s possible that you are changing it in a way that only affects the client (and hence isn’t replicated to the server).
If you print out playerData when saving / loading, what does it show?

I change the values through a LocalScript in the StarterGui. I added some print statements in the DataStore script to print what data it saves, and it prints the default data. However, when I look at the player’s values, it shows changes. Maybe some RemoteEvents could solve the issue?

Use profile service its way easier and more powerful then regular datastore

in that case, the server isn’t seeing any changes. You should make use of RemoteEvents to tell the server to change the value on the server side.

There’s nothing wrong with your datastore script, it’s the way you are changing the value that is the problem.

1 Like

This one is the solution. Mark it as the solution.

I can provide any of you with my level system that includes a super datastore service
You just will have to modify it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.