Problems with Data Stores

I am having problems with my Datastores. And what really confuses me is that there are no errors coming up so I can’t figure it out that way. Would anybody have any solutions?

local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local LeaderStats = Instance.new("Folder", player)
	LeaderStats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue", LeaderStats)
	Cash.Name = "Cash"
	
	local Data
	
	local Success, ErrorMessage = pcall (function()
		Data = MyDataStore:GetAsync(player.UserId.."-Cash")
	end)
	
	if Success then
		Cash.Value = Data
		print(Data)
		print("Player data was successfully retrieved")
	else
		print("There was an error retrieving your data")
		warn(ErrorMessage)
	end 
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Success, ErrorMessage = pcall (function()
		MyDataStore:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
	end)
	
	if Success then
		print("Player data successfully saved")
	else
		print("There was a problem saving the data")
		warn(ErrorMessage)
	end
end)

Data Store Problem Explore View

According to the output it is saying that scripts are being saved and retrieved but the value is simply 0 which it shouldn’t be.

Thanks in advance for any help.

May I know what’s wrong please? That tells me nothing

1 Like
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local LeaderStats = Instance.new("Folder", player)
	LeaderStats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue", LeaderStats)
	Cash.Name = "Cash"
	
	local Data
	
	local Success, ErrorMessage = pcall (function()
		Data = MyDataStore:GetAsync(player.UserId.."Cash")
	end)
	
	if Success then
		Cash.Value = Data
		print(Data)
		print("Player data was successfully retrieved")
	else
		print("There was an error retrieving your data")
		warn(ErrorMessage)
	end 
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Success, ErrorMessage = pcall (function()
		MyDataStore:SetAsync(player.UserId.."Cash", player.leaderstats.Cash.Value)
	end)
	
	if Success then
		print("Player data successfully saved")
	else
		print("There was a problem saving the data")
		warn(ErrorMessage)
	end
end)

You said “-Cash”

local Success, ErrorMessage = pcall (function()
		Data = MyDataStore:GetAsync(player.UserId.."-Cash")
	end)

Is this a fixed version? I can’t tell.

Yes try it and see if it works

No this isn’t the problem because I don’t think it matters what it’s called so long as they are both the same.

Have you enabled api services in game settings

Yes I have done this.

Join in the game and the do “/console” in the chat and tell if there is any errors

No, There are no errors in the output

Try publishing the game and testing it

I have been doing this all along.

I will just rewrite the entire code. And start from fresh.

I have been able to get it working by simply re-writing the entire thing from scratch. Here’s the finish product:

I made two separate scripts one for Leaderstats and one for Datastores

Datastores:

local Players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local cashStore = DataStoreService:GetDataStore("PlayerMoney")

Players.PlayerAdded:Connect(function(player)
	local success, currentCash = pcall(function()
		return cashStore:GetAsync(player.UserId)
	end)

	if success then
		player.leaderstats.Cash.Value = currentCash
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		cashStore:SetAsync(player.UserId, player.leaderstats.Cash.Value)
	end)

	if success then
		print("Success!")
	else
		warn(err)
	end
end)

Leaderstats:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = leaderstats
end)