Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I fix this Issue with a datastore (invalid argument #3 (string expected, got nil)?

Asked by 3 years ago

When I Join the game it keeps say this error
ServerScriptService.DataStorage.RankCreator:36: invalid argument #3 (string expected, got nil):

The error is occuring at line 36

01local DataStoreService = game:GetService("DataStoreService")
02local dataStore = DataStoreService:GetDataStore("DataStore")
03 
04local function saveData(plr)
05 
06    local dataTable = plr.leaderstats("Rank").Value
07 
08    local success, err = pcall(function()
09        dataStore:SetAsync(plr.UserId, dataTable) -- Save the data with the player UserId, and the table we wanna save
10    end)
11 
12    if success then -- If the data has been saved
13        print("Data has been saved!")
14    else -- Else if the save failed
15        print("Data hasn't been saved!")
View all 57 lines...

3 answers

Log in to vote
0
Answered by 3 years ago

You didn't assign any value to data when you define it at Line 29 that's why it returned nil. rank.Value at Line 36 has to be string. Hope this helps!

0
Im pretty sure it defines data at line 31 EhEhEhEhLPKJ 9 — 3y
0
But it's not a string, is it? Dehydrocapsaicin 483 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Success will be true even if there is no data. You will have to check if data exists as well before setting rank.Value.

Log in to vote
0
Answered by
sifn 70
3 years ago

The other answers to this question are indeed telling you the mistake you made, but it does not look like they are being quite clear enough on how exactly to apply the fix. In short, all you need at the very least is an and operator. However, I am going to go a little more in depth than that because I feel that this question stems from a lack of understanding of datastores. Let's go over this.

01local data
02local success, err = pcall(function()
03    data = dataStore:GetAsync(plr.UserId)
04    print("getting data...")
05end)
06 
07if success then
08    rank.Value = data
09else
10    print("The player has no data!")
11end

When making a datastore call like the above, the 'success' variable just indicates if the call made it over the network. So, you could ask for information from a datastore and you may find that the call is successful, but it doesn't mean that there is actually any data saved.

To elaborate on the why a datastore request might fail sometimes (and you might already know this!), when you ask for something over the network such as datastore information, that request has to make it over to Roblox's servers and hence could fail at times, usually because of a bad connection. The success variable tells you if it failed or not and that's its job. Again, it doesn't actually indicate that there is any saved data.

What you want to do is check if the call was successful AND if data was returned, and only then update your value object with the returned data.

1if success and data then
2    rank.Value = data
3else
4    print("The player has no data OR the datastore request failed!")
5end

That's the fix. Very simple. Now, if you wish to handle datastore call failure in some way (retrying the request some number of times, etc.) you could take this a step further by firstly checking if the call was successful, then checking for the existence of saved data, like so:

01if success then
02    print("The datastore request was successful.")
03 
04    if data then
05        print("Saved data found")
06    else
07        print("No saved data found")
08    end
09else
10    print("The datastore request failed: "..err") -- Print the error message to see why the call failed if you're curious!
11end

Answer this question