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)
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.
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