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

Can somebody explain the difference between normal datastore and ordered datastores?

I am learning the Datastore Service on Roblox Studios and is having a hard time understanding the difference between normal Datastore and Ordered Datastore, can somebody please provide an explanation?

Also, what exactly are the difference between a pcall, xpcall, etc?

1 comment

0

This question has been answered effectively before, use the search feature!

1 answer

✓ Accepted answer

Data stores

OrderedDataStores can only store positive numbers

-- OrderedDataStore can save:
500
25000
2138912
1

-- OrderedDataStore cannot save:
-599
'my string'
false or true
Instance.new('Part')
{table}

This data store is mostly used for Leaderboards (Like top 100 best gamers) because it has access to function :GetSortedAsync(), this function will return specified amount of keys stored in the data store sorted from lowest to highest or highest to lowest (You can customize it all in there).

The normal DataStore can save any userdata

-- Data store can save:
true or false
'my string'
5000
-5000
{table}

-- Data store cannot save:
Instance.new('Part')
{dictionary}

-- You see it can save pretty much anything

So this would be used for saving user's data. If i am sure dictionaries are not allowed to be saved in data store but i don't know what a dictionary is :/.. i can surely tell tho that for example this type of table can be saved in the data store for sure:

SetAsync( {
	money = 50000,
	cash = 93184194,
	something = true,
} )

Pcalls

Pcalls (Private Calls) are used to catch errors, they run given function and if it errors then it stops the function, that way you can be sure that if the fucntion errors, your script won't break, example:

--/This function will error because you can't add string to number like that
local function MyFunction()
	local MyVariable = 5 + 'MyString'
end

--/To prevent script from breaking, you use pcall
pcall(MyFunction)

When this will be run, no errors would be shown because the pcall detected the error inside the function and stopped the function, it returns 2 arguments

Success - Whether the function errored or not

Error - Returns error as a string if the function errored, otherwise nil

So in this case the return would be

local function MyFunction()
	local MyVariable = 5 + 'MyString'
end

local Success, Response = pcall(MyFunction)

print(Success, Response)
-- False
-- Attempt to compensate number and string

xpcall is same as pcall but it accepts second argument which is Callback, this argument is a function which will run if the first function in argument 1 errors

local function MyFunction()
	local MyVariable = 5 + 'MyString'
end

local function Callback(Error)
	print( string.format('Oh no! Something bad happened!: %s', Error) )
end

--/Since first function Will error, the Callback[05] function would be run
xpcall(MyFunction, Error)

-- Output:
Oh no! Something bad happened!: Attempt to compensate number and string

Useful Sources:

Datastore v GlobalDataStore

Lua globals

Scripting Helpers - How does pcall, xpcall, and ypcall actually work?

Pcall - Pil

1 comment

1

Pcalls are protected - calls not private calls, but good attempt

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