Help with datastore

so im pretty new to datastore, this script works, its just that sometimes it doesnt save and sometimes it resets the data for no reason

script:

local datastore = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")	

local something = datastore:GetDataStore("stamina")

game.Players.PlayerAdded:Connect(function(player)
	local something3 = Instance.new("IntConstrainedValue")
	something3.Parent = player
	something3.Name = "stamina"
	something3.Value = 100
	something3.MaxValue = 100

	--//\\Load Data//\\
	local data
	local success, errorsomething = pcall(function()
		data = something:GetAsync(player.UserId.. "-stamina")
	end)
	
	if success then
		print("loaded")
		if data then
			something3.MaxValue = data[1]
		end
	else
		warn(errorsomething)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
		
	--//Get Data\\
	local data = {}
	-----------------------------------------------------
	table.insert(data, player.stamina.MaxValue)
	-----------------------------------------------------
	local success, errorsomething = pcall(function()
		something:SetAsync(player.UserId.. "-stamina", data)
	end)
	-----------------------------------------------------
	
	
	
	if success then
		print("saved")
	else
		warn(errorsomething)
		end
end)

if you can help me i would appreciate it. (also this script has 25 values to save overall, its just that i removed most of them so you guys can actually read this)

when setting the data stores, do the maths before setting the values. This way you can be certain that the maths has completed.

Im assuming this is a script to save a players stats like stamina, max health and strength. For this I would recommend putting all the data together and setting / retrieving it at the same time. E.g “Stamina;MaxHealth;Speed”
Then you can use string.split() to access the values quickly.
This means that you can get all the data at once and not have to add more requests to the datastore. This also makes backing up data easier.

I recommend backing up some data, for example each time you update the players stats. You can also update the back up. This means that should something really go wrong e.g mass data corruption or store fails to save properly on loading, that you can then use the backup. It would still mean some loss of progress but it does mean that they wont have to start from scratch.

I would save the error messages in a datastore, I made a script that can basically turn datastores into arrays for storing and reading information. This might be useful for storing error messages and the stats that the user was trying to save. This means that if theres a issue with datastores, you can see common errors, and also manually correct the users data. Ill put a link to my datastore script at the bottom, it might need a bit of adapting but its a freebie anyone can use as they please (Look at the datastore listing functions.)

In terms of using datastores, should things start going wrong you can lose players. This is why you need a backup, and should all else fail, good admin / mod communication with

This is why your values are resetting every time. You should check if they have save data in the datastore. So use :GetAsync() and if it returns nil then set it to them values. Otherwise use the data that was retrieved.

1 Like

they dont reset everytime they barely reset but they sometime do

idk how to use string.split() with datastores, do you have any examples of that

string.split() is not a function of datastore. Its for any string.
Heres an exaple

local Text = "Text1;Text2;Text3"-- First val is the string
local Split = string.split(Text,";") -- Second Value is the seperator
for i = 1,#Split do
    print(Split[i])
end
-- What Split would do to the string
-- {Text1,Text2,Text3}
-- Output would be:
-- Text1
-- Text2
-- Text3

and im sure you know that you can put text together with …
you can add the serperator with … e.g
local Stats = tostring(MaxHealth) … “;” … tostring(Stamina) … “;”
this means that you can save all of it at once and get to the data very easily
all you would have to do is use string.split()
and in this example string.split(Stats,";") Stats[1] is health Stats [2] is Stamina/

Thats because you are checking for success, but when it fails you arent doing anything about it. If success == false then it needs to retry until it has the data.

1 Like

Are you testing this in studio or in game? Also instead of

This method is out dated.
Just do Instance.new(“IntValue”). And there is no MaxValue for a IntValue.