Getting data not working

Hi,

I’m trying to get the data that saves when the player leaves the game. I want it to display the data when the player joins the game but it doesn’t work, it only displays 0.

Server Script:

local DS = game:GetService("DataStoreService")
local scoreStore = DS:GetDataStore("Score")

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(plr)
	local leaderstats = plr:WaitForChild("leaderstats")
	local highscore = leaderstats:WaitForChild("Highscore")

	local success, currentExperience = pcall(function()
		return scoreStore:GetAsync(plr.UserId)
	end)
	if success then
		print(currentExperience) -- only prints 0
	end
end)


players.PlayerRemoving:Connect(function(plr)
	local leaderstats = plr:WaitForChild("leaderstats")
	local timer = leaderstats:WaitForChild("Time")
	
	local success, errorMessage = pcall(function()
		scoreStore:SetAsync(plr.UserId, timer.Value)
		print("saving")
	end)
	if not success then
		print(errorMessage)
	end
end)

Thank you in advanced : )

why are you doin return inside the pcall?

Why is there 2 different thing u want to store?(“Highscore, Timer”)

I follow the roblox Data Stores tutorial

also you’re never really setting the data, that’s what success is used for.

1 Like

I’m new in saving data that’s why I’m following the tutorial
What does success mean?

if you go toyoutube and write “Datastore Roblox studio tutorial” you’ll find good tutorial by alvinblox and thdevking, Idont have much time to explain it right now.

1 Like

Success is the bool that returns true if the function inside the pcall run’s properly like if GetAsync() successfully retrieves data or if SetAsync() properly set’s data. If success is false then that means whatever function you ran returned an error, thus the errorMessage in

local success, errorMessage = pcall(function()
		scoreStore:SetAsync(plr.UserId, timer.Value)
		print("saving")
	end)

is the error that returned if scoreStore:SetAsync() failed

1 Like

Are you sure the value is not 0 on the server?
You might be changing it on your own client while testing.

1 Like

Compare your code to mine and see if this works.
This tutorial also explains pcalls!

1 Like