How to put Datastore:GetAsync() in a pcall?

i want to pcall DataStore:GetAsync() so that players dont lose data. Whichever way i do i t, i seem to get an error. What am i doing wrong here?

    local success, savedList = pcall(get, plr.userId.."Import")
	
	while not success do wait(1)
		print(savedList)
		success, savedList = pcall(get, plr.userId.."Import")
	end

also, that wait increment up there for one second may be too short or may not be, and possibly expend the budget for writing to the datastore ^^^, if anyone has anything to say about that, im all ears

EDIT: Sorry for the lack in information xd. get is declared as

game:GetService("DataStoreService"):GetDataStore("PlayerData").getAsync

The error returned by the pcall is “Expected ‘:’ not ‘.’ calling member function GetAsync”
@incapaxx

What is the exception?

And what is get declared as?

You’re getting this exception because you didn’t properly pass self.

Remember that

object:method(...)

Is the same as

object.method(object, ...)

The former is syntactic sugar for the latter. When using the : notation, you implicitly pass the object you’re calling it on as the first argument.

You could pass any argument as self when using the . notation.

workspace.Baseplate.Destroy(workspace.Part)

And that would delete the part, and not the baseplate.

local success, data = pcall(a.b, a, ...)
4 Likes

Omg that worked xd. Thank you @incapaxx

jeez, never knew that pcalls were such a syntactical nightmare

They aren’t that much of a nightmare. If you directly wrap a function (which is how pcall should be used), then yes you need to be mindful of the syntax and the arguments that need to be passed. Otherwise, you can wrap an anonymous function and call it normally.

local success, savedList = pcall(function ()
    return get(args)
end)
1 Like