Datastore stopped saving

I made an autosave script for datastore but it suddenly stopped saving for some reason.
CODE:

game.Players.PlayerRemoving:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
	local statstorage = player:FindFirstChild("Data"):GetChildren()
	for number =  1, #statstorage do
		datastore:SetAsync(statstorage[number].Name, statstorage[number].Value)
	end
	print(player.Name .. "'s stats saved")	
end)

game.Players.PlayerAdded:connect(function(player)
		local folder = Instance.new("Folder",player)
	folder.Name = "Data"
	local Nex = Instance.new("NumberValue",folder)
	Nex.Name = "Nex"
	Nex.Value = 100
		local Level = Instance.new("NumberValue",folder)
	Level.Name = "Level"
	Level.Value = 1
			local XP = Instance.new("NumberValue",folder)
	XP.Name = "XP"
	XP.Value = 0
				local Faction = Instance.new("StringValue",folder)
	Faction.Name = "Faction"
	Faction.Value = "None"
	
	
	wait(1)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats")
	player:WaitForChild("Data")
	wait(1)
	local stats = player:WaitForChild("Data"):GetChildren()
	for number = 1, #stats do			
		stats[number].Value = datastore:GetAsync(stats[number].Name)
	end
	print(player.Name .. "'s stats loaded")	
end)

Error:

22:33:56.423 - ServerScriptService.Stats.Save n load:34: bad argument #3 to 'Value' (string expected, got nil)
1 Like

Is there anything in the output? Most likely if anything goes wrong, you’ll be told about it.

Also it’d be a good idea to format your code properly so it is easier to understand.

For example, you have what appear to be normally indented ends, however there are more ends at the bottom which aren’t indented at all. This gives the wrong impression for what is done in the code, and makes it hard to figure out which end corresponds to what.

Example of good indentation:

function name()
    if condition then
        signal:Connect(function()
            -- code
        end)
    end
end

I can see a potential error where the wrong end has ) at the end, however without formatting I cannot tell for sure at this time of night for me lol

Also, for the folder creation part, it’d be much easier to understand and write if you simply put an if statement checking whether WaitForChild(“Data”) got a result or not

its formatted properly on my screen and there are no errors on output

Can we see the formatted version then? The formatting on the code here isn’t right. You should be able to get the right formatting by just copying your code directly into the text box with ``` before and after your code.

Try adding prints to code to see where it reaches during the saving/loading/etc. process
Also note that debugging using prints is really useful for future issues you come across :wink:

image

Its formatted omg, fix your screen pls

I can’t really read your code, it’s super messy :hushed:

4 Likes

I think I might know the problem
Commas and semicolons act mostly similar in tables, meaning you’re saving the individual values suck as “Nex”, “StringValue”, etc. Try saving them like this;

local stuff = {
     {"Nex", "NumberValue", 100},
     {"Level", "NumberValue", 1},
     {"XP", "NumberValue", 0),
     {"Faction", "StringValue", "None"}
}

You’d access them somewhat the same, but to get Nex, for example, you’d do stuff[1][1]

I started debugging it and found this error

21:40:53.909 - 502: API Services rejected request with error: HTTP 0 (CURL error (curl_easy_perform): Timeout was reached (28))

EDIT: Increase delay from 3 to 6 and I don’t get this error anymore, however, data still isn’t saving.

Dear all, I have created a new script but it does not work, please review the updated top description.

The code provided clearly isn’t all the code that produced the error given in the OP, it says the error is on line 39 whereas the code provided only has 37 lines.

Also, your code still isn’t perfectly indented and there appears to be quite a few redundant wait()s. If you could point out exactly what line is causing this error, we’d be able to help a lot faster, otherwise we have to look at everything which takes time.

Edit: This is incorrect indentation, it makes it look very messy having random lines more indented than others. It’s bad practice too as some languages require perfect indentation.
indent

1 Like

It’s indented on my screen what are you guys talking about. It’s either my mac or devforum glitching out again

Error happens on this line btw

stats[number].Value = datastore:GetAsync(stats[number].Name)

In that case, the error is occurring because the data is nil and you’re trying to set a string value to nil which throws that error. The reason for the error being nil is probably because you’ve never had that data saved.

Replacing that line with this should resolve that error:

stats[number].Value = datastore:GetAsync(stats[number].Name) or ""

If you encounter any more errors, let us know!

2 Likes

After I used that, all the values in my data folder are nil, hmm

I changed the datastore name and now only the faction value is nil for some reason

This isn’t printing either
print(player.Name … “'s stats saved”)

Whoops, I forgot about keeping the default data if it’s nil. Use this instead:

stats[number].Value = datastore:GetAsync(stats[number].Name) or stats[number].Value

The reason your data saved as nil is because it was saving the empty StringValue values. Change the datastore key and try again. 🤦

2 Likes

No errors now but it’s not printing that my data is saved. Data isn’t actually saved either

it prints before this line but not after

	for number =  1, #statstorage do
		datastore:SetAsync(statstorage[number].Name, statstorage[number].Value)
	end

Oops, i was saving them as two different datastores, thanks!

Glad to hear you got it working, also I’d recommend using player.UserId as a unique key instead of player.Name otherwise if people change their usernames they’ll lose their data.

Ok so i replace name with userid?

Needs to be a string. So instead of using

player.Name

Use

tostring(player.UserId)
1 Like