Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

datastore not functioning, right after it loads it turns to zero?

If you don't want to help, don't help, for those who want to help; try your best :), and if you can't dont leave an answer.

#Problem

It's supposed to work like a normal datastore, but obviously things never work for me so there was something wrong; When my friend joined, i saw his old datastore go to his for a split second and then it went back to zero for a reason?

#Script

local DataStoreService = game:GetService("DataStoreService")
local StatsData = DataStoreService:GetDataStore("Savers")

local key = "Player_"

game.Players.PlayerAdded:Connect(function(player)
	local sucess, errormessage = pcall(function()
		local retrievedData = StatsData:GetAsync(key .. player.UserId)
		local leaderstats = player.leaderstats
		
		leaderstats.Cash.Value = retrievedData[1]
		leaderstats.Power.Value = retrievedData[2]
		leaderstats.Rebirths.Value = retrievedData[3]
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local leaderstats = player.leaderstats
	
	local sucess, errormessage = pcall(function()
		StatsData:SetAsync(key .. player.UserId, {leaderstats.Cash.Value, leaderstats.Power.Value, leaderstats.Rebirths.Value})
	end)
	
	if sucess then
		print("Was a succesfull data store!")
	else
		warn(errormessage)
	end
end)

2 comments

0

Try using a loop; this article may help you: https://developer.roblox.com/en-us/articles/Saving-Player-Data

1

Why does people downvote this. She did nothing boi

1 answer

✓ Accepted answer

Datastores may fail to work sometimes, so you need to keep saving/loading data until it's successful.

local tries = 0 -- For debugging

if sucess then
	print("Success")
else
	while not sucess do
		print("Error: ", errormessage)
	sucess, errormessage = pcall( -- GetAsync/SetAsync function 
	)
	tries = tries + 1
	wait() -- Prevent a script timeout error
	end
end

print("Attempts to save/load: ", tries)

This discussion is preserved as it was. New answers, comments, and votes are closed.