Datastore UpdateAsync Error (Unable to cast value to function)

print("update")
print(userId.." | "..value)

local success, response = pcall(topPlayerVisits.UpdateAsync, topPlayerVisits, userId, 
    function(oldValue) 
        return value 
    end
)
    

if response ~= nil then
    warn(response.. " update async")
end

print("success")

Nothing is printed except for warn and error is unable to cast value to function. I’ve checked all the other topics but I couldn’t solve the problem

That looks right, though this form of using pcall can get unreadable sometimes. This thread seems to be lacking information, could you provide more details about the error? On exactly which line is it occurring and what are you performing on that line? It may be an issue of the value you’re trying to give to UpdateAsync.

It isn’t an error it’s a warn from my UpdateAsync pcall. And the value is an int value which is got from the PlayerAdded event leaderstats.

Yes, “Unable to cast x to y” is an error. If that’s coming from the warn then the function you put in pcall failed to run. There’s a reason why a success value is also returned from a pcall and it’s important to use that in determining whether the function ran successfully or not rather than assuming the call was successful and ignoring it.

okay let me try something hold on

So I did this

print("update")
print(userId.." | "..value)

local success, response = pcall(topPlayerVisits.UpdateAsync, topPlayerVisits, userId, 
    function(oldValue) 
        return value 
    end
)
    
if success then
    print("success")
else
    warn(response.. " update async")
end

but still the same, changed the print for success, response.

Topic solved turns out I don’t know how team create works and we need to close the script to save the changes :frowning:

1 Like