Getting an error on a pcall function (Unable to cast value to object)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a data saving system for aura storing.

  2. What is the issue? Include screenshots / videos if possible!
    Everytime I press play, I get an error. (Duo testing and it works for my friend but not me)
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried reopening studio, changing the code and looked for solutions on the developer forum but I did not find anything.

local data
local success, errormessage = pcall(function() 
	data = playerdataservice:GetAsync(plr.UserId, dataname)[object] -- Error here :c
end)
if success and data then
	print("Data found.")
	return data
else
	return print("No data was found.")
end
Pictures

The code:


The error:
image

By the way this is in a module script.

1 Like

Is playerdataservice another module or is it a datastore object? If the latter, the second argument of :GetAsync is a DataStoreGetOptions which I’m guessing dataname is not, you should be able to omit it from your GetAsync call entirely

1 Like

It is the store that I use to save the player’s data.

local playerdataservice = dataservice:GetDataStore("PlayerExperience")

The function is not being used yet I am still getting this error. I thought that it had to be used first, but I could not find anything close to PcallStore. (the function)
And what do you mean by DataStoreGetOptions?

DataStoreService:GetDataStore() returns a GlobalDataStore object, the error here is caused by attempting to call GlobalDataStore:GetAsync() with the second argument being invalid
basically DataStoreGetOptions is an instance that lets you change how the datastore is accessed, which currently is just for disabling caching on requests for it

the problem here is that GetAsync()'s second parameter takes either nothing or a DataStoreGetOptions instance, if you simply remove the second argument it should work
what i think you meant to do is specify a scope for the get request, which is done in DataStoreService:GetDataStore() instead in the second argument, or by adding a prefix to the first argument of GetAsync() (example: scope/key)

But I am not using that function at all, meaning that not a single argument is a value/object. It just gives me an error somehow.

i’m a bit confused by what you mean, try placing breakpoints around your script to figure out exactly where the error is since pcall could be throwing away the stack trace

I’m guessing this is to load data and you want to get an object of that data.

Try doing this instead and see if it works:

local success, data = pcall(function() 
	return playerdataservice:GetAsync(plr.UserId) -- no need for dataname variable
end)
if success and data then
	print("Data found.")
	return data
else
	return print("No data was found.")
end

I’m unsure of what the object is, but I’m sure when you return the data you can simply just do:

local data;
local foo= PcallStore();

if foo then
	data = foo[object];
end

Chances are you aren’t using it so just omit the dataname arg entirely

Sure, I’ll try that right now! :happy1:

I’ve tried that already.

I’m not sure where to put this… :sad:

I’m not even using the function at all, meaning that I cannot define/use dataname.

I’m still getting the error with the break points and it just sends me to the pcall function.

I tried this again but changing it up a little bit, and it sort of worked… I’m still not sure if it would work as I expected it to, but I hope it does work once I make the function add data. (maybe I already have that, I just forgot?)

Edit: I’m gonna mark your post as the solution, but I’m still not sure if this will work. If it is not gonna work later, then I’m probably gonna undo it.

Oh, I found the part where the function is being used. And dataname is a string value, same thing with object, but the plr argument is the player.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.