A better way to store data?

i just need a quick solution i didnt realize it was this advanced

1 Like

If your DataStore is otherwise working correctly then I recommend watching this YouTube tutorial by 5uphi that should help explain to you how to compress the data before storing it into your DataStore: Compress / Optimize Datastore - Roblox Scripting Tutorial

3 Likes

It’s probably the quickest solution out there. You can use DataStore2 if you want to.

1 Like

how do i use pcall in my script?

1 Like

pcall is a function that can catch an unexpected error in the code.

Basic example:

local x = 5
local a = "1"
local success, err = pcall(function()
 x += a
end)
print(success, err)

It will catch the error and return 2 values: if the function execution was successful and the error message (nil if there’s no error)

1 Like

https://devforum.roblox.com/t/save-your-player-data-with-profileservice-datastore-module/667805

ProfileService is quite easy to set up, ive personally never had any issue with it

1 Like

how would that help with storing data?

1 Like

You asked what pcall does. I gave you a basic example.
Basically, instead of doing Asyncs by themselves, wrap them in a pcall

function setAsync(ds:DataStore, key:any, value:any)
 local success, err = pcall(function()
  ds:SetAsync(key, value)
 end)
 if not success then
  warn(err)
  wait(1)
  setAsync(ds, key, value)
 end
end

And then instead of leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData) do setAsync(leaderstatsDataStore, player.UserId, leaderstatsData)

Same goes for getting async.

Edit: Added a delay so that you won’t get rate limited

i kind of get it, but how would that apply to my script? i just add local success, err = pcall(function()
to it?

1 Like

Insert the function somewhere at the beginning of the code and simply replace setAsync with it

replace setAsync with pcall thats all??

game.Players.PlayerRemoving:connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		if loaded[player] then
			local leaderstatsData = {}
			for i, stat in ipairs(leaderstats:GetChildren()) do
				leaderstatsData[stat.Name] = stat.Value
			end
			task.spawn(function()
				setAsync(leaderstatsDataStore, player.UserId, leaderstatsData)
			end)
		end
	end
	loaded[player] = nil
end)

so i replace

		task.spawn(function()
			setAsync(leaderstatsDataStore, player.UserId, leaderstatsData)

with pcall?

Insert this function. Just replace your old one with this. It’s already done.

I won’t be able to respond no more because it’s midnight

oh okay! thank you! i will try it now

setAsync(leaderstatsDataStore, player.UserId, leaderstatsData)
gives me a red underline, and nothing seemed to change but thanks!

If it’s underlined red there’s most likely a syntax error. You can just point at the red line, wait a couple of seconds and it should tell you where the syntax error is.

can you help me with something i want to add a instance to it but its bugged out

create a post under scripting support, dont ask here

i did multiple times noone for real answer