Hi everyone, I've started to migrate away from Data Persistence and move on to Data Stores. I couldn't get my head round the whole Data Store concept at first, but now that I have, I have constructed a script to create values, and to set their values to 0 if the GetAsync method returns nil, otherwise it will return the given value from the GetAsync method. I also have it to set the values of keys when their corresponding values inside the value objects change.

However, I'm testing this script with the player/server mode (I used SetPlaceId on my locally saved place) and it seems to be giving me an error that I can't wrap my head around on line 10 of the script at the bottom of the post.

Attempt to index upvalue ds (a nil value)

I thought I knew how to fix this, but I can't fix it so that it still checks if the GetAsync method returns nil or not. So I'm kind of stumped right now.

The code below is in a Script inside of ServerScriptStorage:

local ds = game:GetService("DataStoreService"):GetDataStore("SLStats")

game.Players.PlayerAdded:connect(function(player)
	local playedKey = "Played_" .. player.userId
	local wonKey = "Won_" .. player.userId 
	
	local played = Instance.new("IntValue")
	played.Name = "PlayedGames"
	played.Parent = player
	if ds:GetAsync(playedKey) == nil then --This line errors, and I don't know why.
		played.Value = 0
	else
		played.Value = ds:GetAsync(playedKey)
	end
	
	local won = Instance.new("IntValue")
	won.Name = "WonGames"
	won.Parent = player
	if ds:GetAsync(wonKey) == nil then --Same as the other if statement, so this would error but I don't know why.
		won.Value = 0
	else
		won.Value = ds:GetAsync(playedKey)
	end
	
	played.Changed:connect(function(val)
		ds:SetAsync(playedKey, val)
	end)
	
	won.Changed:connect(function(val)
		ds:SetAsync(wonKey, val)
	end)
end)

Any help is appreciated, thank you!

2 comments

2

I believe that is because you are on a local server.

1

Oh right, I would've thought it would have worked. I used the SetPlaceId method before I started the server. Thanks.