Datastore service script 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 have a simple datastore system that should save player data when leaving and rejoining.

  1. What is the issue? Include screenshots / videos if possible!

So there is a guide on Datastore for beginners. I saw their code was very overcomplicated and had way too many variables (in all 3 functions they restate the datastoreservice 3 times in total) so I decided to simplify it. (Code below) However, something seems to be off with my code. It doesnt not work. I just cannot seem to find the reason why; I have functions that get data, load, and a serverscript along with it. The pcalls don’t return any errors either. I receive that UserID is not a valid member of players. This may be the only error or the rest of the code can have many errors. The devforum I followed shows the code they used, and it works, but I cannot understand why mine doesn’t. I need help understanding and first finding my mistake. Here is their devforum I followed which contains all their code: How To Use, Utilize, and Succeed with DataStores

  • List item
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Yes, I have followed a Devforum guide, tried their code and mine, compared. Their did while mine didn’t. I did some research on pcalls and get async. I keep getting these errors despite my efforts it’s difficult. It’s definetely frustrating; this always happens when I try making long script and some error ruins the entire thing

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

The modulescript:



local Datamodule = {}

local DataStoreService = game:GetService("DataStoreService")
local Datastore = DataStoreService:GetDataStore("Datastore")
local key 
local data
function Datamodule.SetData(player)
	-- create a starting leaderboard


	local ldrstats = Instance.new("Folder")
	ldrstats.Name = "leaderstats"
	
	local Points = Instance.new("IntValue")
	Points.Value = 0

	Points.Parent = ldrstats
	ldrstats.Parent = player



end




function Datamodule.SaveData(player)
	
	
	key = player.UserID
	data = player.leaderstats.Points.Value
	local success, result
	-- save our data upon client disconnect gg ez


	repeat
	success, result = pcall(function()
		Datastore:UpdateAsync(key, function() -- from the datastore, make a update async function that takes a key and a function to return the data. 
			return data
		end)
	end)
	task.wait()
	until success


end

function Datamodule.LoadData(player)
	
	local PlayerService = game:GetService("Players")
	
	
	key = player.UserID
	
	local success, result
	
	repeat
		success, result = pcall(function()
			data = Datastore:GetAsync(key)
		end)

		task.wait()
	until success or not PlayerService:FindFirstChild(player.Name)
	
	if success then
		player.leaderstats.Points.Value = data
		print("Successfully loaded the data for user " .. player.Name)
	elseif not success then
		warn("An error occurred while trying to load the data, see here for more: " .. tostring(result))
	else
		warn("An error occurred, see here for more: " .. tostring(result))
	end
end




return Datamodule

The server script
```lua
local DSModule = require(game.ServerScriptService.DatastoreModule)

local addInstances = DSModule.SetData
local saveData = DSModule.SaveData
local loadData = DSModule.LoadData

local PlayerService = game:GetService("Players")

-- // Player Added Event
game.Players.PlayerAdded:Connect(function(player)
	addInstances(player)
	loadData(player)
end)

-- // PlayerRemoving Event
game.Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

game:BindToClose(function()
	for i, player: Player in ipairs(PlayerService:GetPlayers()) do
		saveData(player)
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

key = player.UserID

should be player.UserId and should be a string

tostring(player.UserId)
1 Like

OHHH, I see now. I made a typo. Another one is with points; i did leaderstats.Points when i didn’t set the name to “Points” so it was just called Value. Thank you, now I will make sure to check my code for any spelling errors for the future.:pray:

1 Like

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