Still having problems with understanding DataStores

Hello!

So recently, I have been working with Data Stores, and trying to save stuff. Unfortunately, there is a problem.

I don’t get why these errors occur, am I doing something wrong?

Here is the code:

local dataStoreService = game:GetService("DataStoreService")
local testDatastore = dataStoreService:GetDataStore("dataSwordTest")

local testString = "dataSwordString"
local swordNumber
local sworditems

local defaultNumber = 0
local globalChar

game.Players.PlayerAdded:Connect(function(plr)
	if plr then
		local chr = plr.Character or plr.CharacterAdded:Wait()
		globalChar = chr
		sworditems = chr:WaitForChild("sworditem(s)")
		swordNumber = sworditems:WaitForChild("swrdNumber")

		local data
		local success, fail = pcall(function()
			data = testDatastore:GetAsync(testString)
		end)

		if success and data then
			print("Loaded sword data.")
			swordNumber.Value = data
		else
			print("Failed to load data.")
			swordNumber.Value = defaultNumber
		end

	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		local success, response = pcall(function()
			testDatastore:SetAsync(testString, swordNumber.Value)
		end)

		if success then
			print("data saved")
		else
			warn("Error.")
		end
	end
end)

Im just lost right now. The issue is, the Data Store is having problems with loading and saving the value, and I don’t seem to know why. Any help is appreciated.

This is what happens when I join the game:

This is what happens when I leave the game:

Screenshot 2023-08-13 201459

1 Like

You need to use game.Players.PlayerRemoving with game:BindToClose()

:BindToClose() is used when the server shuts down for x,y,z reasons. It is not used, when a player leaves, if the server is still Active. :BindToClose only fires once per server, right before it shutting down . However, if the server shuts down, sometimes Players.PlayerRemoving never fires! Meaning data loss. So its important you don’t use one or the other, you use both together .

2 Likes

Its still giving me the errors.

Also you need a unique key to save and load data with a player. A unique key allows different users to save their data to the same datastore. You can fix this with using the UserID of a player:

testDatastore:SetAsync(testString..player.UserId, swordNumber.Value)
testDatastore:GetAsync(testString..player.UserId)

if your still having issues, please send over your current updated script, as well as any updated info

Here is my current script:

Notice, I am trying to save a value that is inside of the character instead of the player, could that be why?

Or could I be that I am changing the value from the character inside of a LocalScript? idk.

local dataStoreService = game:GetService("DataStoreService")
local testDatastore = dataStoreService:GetDataStore("dataSwordTest")

local testString = "dataSwordString"
local swordNumber
local sworditems

local defaultNumber = 0
local globalChar

game.Players.PlayerAdded:Connect(function(plr)
	if plr then
		local chr = plr.Character or plr.CharacterAdded:Wait()
		globalChar = chr
		sworditems = chr:WaitForChild("sworditem(s)")
		swordNumber = sworditems:WaitForChild("swrdNumber")

		local data
		local success, fail = pcall(function()
			data = testDatastore:GetAsync(testString .. plr.UserId)
		end)

		if success and data then
			print("Loaded sword data.")
			swordNumber.Value = data
		else
			print("Failed to load data.")
			swordNumber.Value = defaultNumber
		end

	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, response = pcall(function()
		testDatastore:SetAsync(testString .. player.UserId, swordNumber.Value)
	end)

	if success then
		print("data saved")
	else
		warn("Error.")
	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		local success, response = pcall(function()
			testDatastore:SetAsync(testString .. player.UserId, swordNumber.Value)
		end)

		if success then
			print("data saved")
		else
			warn("Error.")
		end
	end
end)

The script is also located in ServerScriptService, so should I move it?

yes. you need to change any values your gonna save to a datastore, changing it in a server script. They won’t see any changes from the client

you can also try and adding prints before saving it. For ex. is swordNumber.Value returning nil? Is the data variable returning nil on join?

Man, ima just mark your solution as correct.

Although you tried to help me, I am super dumb myself.

Guess what I forgot to turn on… API Services. Now everything is working.
Sorry for wasting your time.

No worries. There was still some things that I corrected that were important, such as the BindToClose. Glad you were able to fix it though!

1 Like

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