DataStore Does not work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a game and i want to make datastore to save player stats
  2. What is the issue? Include screenshots / videos if possible!
    For some reason when the Player leaves the game the data is not being saved
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried checking some solutions but none of them work

there is no errors on the output

local AmountDataStore = game:GetService("DataStoreService"):GetDataStore("Test") -- "Test" for Testing, "Production" for updating/releasing

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local Wins = Instance.new("NumberValue", leaderstats)
	Wins.Name = "Wins"
	Wins.Value = 0
	
	local Loses = Instance.new("NumberValue", leaderstats)
	Loses.Name = "Loses"
	Loses.Value = 0
	
	local Points = Instance.new("NumberValue", leaderstats)
	Points.Name = "Points"
	Points.Value = 100
	
	local WinsData
	local LosesData
	local PointsData
	
	local succes,errormessage = pcall(function()
		WinsData = AmountDataStore:GetAsync("Wins_"..plr.UserId)
		LosesData = AmountDataStore:GetAsync("Loses_"..plr.UserId)
		PointsData = AmountDataStore:GetAsync("Points_"..plr.UserId)
	end)
	
	if succes then
		if WinsData and LosesData and PointsData then
			Wins.Value = WinsData
			Loses.Value = LosesData
			Points.Value = PointsData
		else
			print("Data is not found or corrupted")
		end
	elseif errormessage then
		print("There has been an error loading the data, error: "..errormessage)
		plr:Kick("Sorry, but your data has not loaded succesfully, try again or contact the owner with this error message: "..errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local succes,errormessage = pcall(function()
		AmountDataStore:SetAsync("Wins_"..plr.UserId, plr.leaderstats.Wins.Value) -- the script stops here
		AmountDataStore:SetAsync("Loses_"..plr.UserId, plr.leaderstats.Loses.Value)
		AmountDataStore:SetAsync("Points_"..plr.UserId, plr.leaderstats.Points.Value)
	end)

	if succes then
		print("Succesfully Saved")
	elseif errormessage then
		print("There has been an error saving the data, error: "..errormessage)
	end
end)

I dont know what to add.
But here is the output when i join:
image
And when I leave the game, it does not print anything.

3 Likes

wait i have another thing.
I uninstalled a plugin, when i play test and left it said data saved, and now when i test it again it does not print anything?

2 Likes

Have you had a chance to test this in Roblox Studio? The reason I’m asking this is because, over the past few months, I’ve noticed that Roblox Studio not has been getting data for datstores.

4 Likes

What I would be doing is something like this where it can get the players data when they join.

local DataStoreService = game:GetService("DataStoreService")
local AmountDataStore = DataStoreService:GetDataStore("Test")

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"

	local Wins = Instance.new("NumberValue", leaderstats)
	Wins.Name = "Wins"

	local Loses = Instance.new("NumberValue", leaderstats)
	Loses.Name = "Loses"

	local Points = Instance.new("NumberValue", leaderstats)
	Points.Name = "Points"

	local success, errorMessage = pcall(function()
		Wins.Value = AmountDataStore:GetAsync("Wins_"..plr.UserId) or 0
		Loses.Value = AmountDataStore:GetAsync("Loses_"..plr.UserId) or 0
		Points.Value = AmountDataStore:GetAsync("Points_"..plr.UserId) or 100
	end)

	if success then
		print("Data successfully loaded")
	else
		print("There was an error loading the data: " .. errorMessage)
		plr:Kick("Sorry, your data could not be loaded. Please try again or contact the owner. Error: " .. errorMessage)
	end
end)


game:GetService("Players").PlayerRemoving:Connect(function(plr)
	local success, errorMessage = pcall(function()
		AmountDataStore:SetAsync("Wins_"..plr.UserId, plr.leaderstats.Wins.Value)
		AmountDataStore:SetAsync("Loses_"..plr.UserId, plr.leaderstats.Loses.Value)
		AmountDataStore:SetAsync("Points_"..plr.UserId, plr.leaderstats.Points.Value)
	end)

	if success then
		print("Data successfully saved")
	else
		print("There was an error saving the data: " .. errorMessage)
	end
end)
2 Likes

Otherwise what you can do is use Datastore2

local DataStore2 = require(1936396537)

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local winsData = DataStore2("Wins_", plr)
	local losesData = DataStore2("Loses_", plr)
	local pointsData = DataStore2("Points_", plr)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local wins = Instance.new("IntValue", leaderstats)
	wins.Name = "Wins"
	local loses = Instance.new("IntValue", leaderstats)
	loses.Name = "Loses"
	local points = Instance.new("IntValue", leaderstats)
	points.Name = "Points"

	local success, errorMessage = pcall(function()
		wins.Value = winsData:Get() or 0
		loses.Value = losesData:Get() or 0
		points.Value = pointsData:Get() or 100
	end)

	if success then
		print("Data successfully loaded")
	else
		print("There was an error loading the data: " .. errorMessage)
		plr:Kick("Sorry, your data could not be loaded. Please try again or contact the owner. Error: " .. errorMessage)
	end
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)
		local WinsData = DataStore2("Wins_",plr)
		local LosesData = DataStore2("Loses_",plr)
		local PointsData = DataStore2("Points_",plr)
	local success, errorMessage = pcall(function()
		WinsData:Set(plr.leaderstats.Wins.Value)
		LosesData:Set(plr.leaderstats.Loses.Value)
		PointsData:Set(plr.leaderstats.Points.Value)
	end)

	if success then
		print("Data successfully saved")
	else
		print("There was an error saving the data: " .. errorMessage)
	end
end)

3 Likes

Yes, I tested it in Roblox studio.

2 Likes

Thank you very much for your support!

3 Likes

it’s because the player doesn’t have data yet. you don’t need datastore2 for this but if you want to use it then ok

2 Likes

why you pcall()ing asynchronous functions???

1 Like

because it might cause error??

1 Like

bro what??? asynchoronous functions are already handling any errors
so theres no need to pcall() asynchronous functions

you have a point but some of them are like SetAsync

i swear i saw a devforum post of using pcalls in asynchronous functions but dont remember the exact post

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