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

The Datastore don't save, why?

I am making a DataStore so when the player leave the leaderstats save, my problem is: when i rejoin the data don't save this is my script

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_xd"..plr.userId
	local save1 = plr.leaderstats.Money
	local save2 = plr.leaderstats.Strength
	local save3 = plr.leaderstats.Rebirths
	
	local GS = ds:GetAsync(plrkey)
	if GS then
		save1.Value = GS[1]
		save2.Value = GS[2]
		save3.Value = GS[3]
	else
		local NFS = {save1.Value, save2.Value, save3.Value}
		ds:GetAsync(plrkey, NFS)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_xd"..plr.userId, {plr.leaderstats.Money.Value, plr.leaderstats.Strength.Value, plr.leaderstats.Rebirths.Value})
end)

help please!

5 comments

0

Tip: You should always wrap your functions in pcall.

0

Have you already made the leaderstats?

0

yes

0

You must use a Server-sided Script (commonly called a "regular" script); preferably located in ServerScriptService.

0

i have it in ServerScriptService

1 answer

Some changing(s) could be made. Like this:

In the player added event (I will make pcalls too as you should do it that way):


local data
local success, errormessage = pcall(function()

	data = ds:GetAsync(plrkey)

end)

if success and data then
	save1.Value = data.Save1
	save2.Value = data.Save2
	save3.Value = data.Save3
else
	print("Could not fetch data!")
	warn(errormessage)
end

That's done for the player added part. Inside the player removing part, I will add a table, call it data. And save the values there. Like below (player removing):

local success, errormessage = pcall(function()
	local data = {
		Save1 = plr.leaderstats.Money.Value;
		Save2 = plr.leaderstats.Strength.Value;
		Save3 = plr.leaderstats.Rebirth.Value;
	}

	ds:SetAsync(plr.UserId, data)
end)

if success then
	print("Data successfully saved")
else
	print("There was an error saving the data.")
	warn(errormessage)
end

That should do it.

21 comments

0

don't work :/

0

Any errors in output?

0

yeah this: Could not fetch data! 23:56:34.428 - ServerScriptService.SaveData:6: attempt to index nil with 'GetAsync'

0

Oh sorry, my bad. Fixed the script now.

0

Instead of saying SaveData, say ds

0

now it says leaderstats is not a valid member of player.(myname)

0

nevermind i fix it

0

nevermind i fix it

0

Please accept the answer if it helped you :)

0

yeah nevermind it i add wait() and fix it, but in ouput say this: Could not fetch data! 00:12:19.144 - nil There was an error saving the data. 00:12:57.656 - 104: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters.

0

For the first time, when there is not data, it says this. Even to me. But when there is data, then it does not say this

0

Nil means that there is no data

0

ok, when it have data it and i close it that says: 00:17:37.747 - 104: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters.

0

Oh okay. Got it again. Editted the answer.

0

Did you add the .Value at the end?

0

is the same error :/

0

ok i add .Value and ouput get nil and don't save

0

Ok. Make sure you are testing this in a game. (not roblox studio). When I try in studio, it sometimes does not save. But in real game, it should always work. Try it and tell me

0

oh

0

i try it and no :/

0

Let me give you a tip: why don't you make the leaderstats inside the same script? That might work.

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