Data store help!

I have followed Alvin Blox’s and TheDevKing’s tutorial on data stores twice! I just can’t get it to work!

It keeps printing the ‘success’ message but it also throws an error:
21:58:34.841 - ServerScriptService.DataStore:54: invalid argument #3 (string expected, got nil)
This is due to the ‘data’ saved is somehow nil, and yes… API is enabled

Some extra info:
This is a speedrunning game and every level has a ‘Timer’ value, this is what I want to save.
robloxxx
Don’t worry about the rest…

Script

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	--[ Add Player Info ]--
	local playerFolder = Instance.new("Folder")
	playerFolder.Name = tostring(player)
	playerFolder.Parent = game.ServerStorage
	
	local inLevel = Instance.new("BoolValue")
	inLevel.Name = "InLevel"
	inLevel.Parent = playerFolder
	
	local inRun = Instance.new("BoolValue")
	inRun.Name = "InRun"
	inRun.Parent = playerFolder

	local endPart = Instance.new("ObjectValue")
	endPart.Name = "endPart"
	endPart.Parent = playerFolder
	
	local PersonalBests = Instance.new("Folder")
	PersonalBests.Name = "PersonalBests"
	PersonalBests.Parent = playerFolder

	local levels = game.Workspace:FindFirstChild("TeleportService").WorldOne:GetChildren()
	
	for _, level in pairs(levels) do
		local newLevel = Instance.new("StringValue")
		newLevel.Name = level.Name
		newLevel.Parent = PersonalBests
	end

	local levelValues = PersonalBests:GetChildren()
	
	for _, level in pairs(levelValues) do
		local toVal = Instance.new("NumberValue")
		toVal.Name = "NumberValue"
		toVal.Value = 999999999
		toVal.Parent = level
	end
	
	local playerUserId = "player_" .. player.UserId
	
	local data
	local success, err = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)

	if success then
		print("SUCCESS!!")
		PersonalBests.Level1.Value = data
	else
		print("Complete failiure! :(")
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local playerUserId = "Player_" .. player.UserId
	local data = game.ServerStorage:FindFirstChild(player.Name).PersonalBests.Level1
	print(data)
	
	local success, err = pcall(function()
	myDataStore:SetAsync(playerUserId, data.Value)
	end)
	
	if success then
		print("Succesfuly saved!")
	else
		print("There was an error!")
		warn(err)
	end
end)

Hi there, it would be extremely helpful if you pointed out which line of your code is line 54 (as it states in the error that it’s line 54 and it would make troubleshooting much faster!).

1 Like

Additionally I noticed you are saving under the key: local playerUserId = "player_" .. player.UserId on your PlayerAdded connection, but you then go on to save under local playerUserId = "Player_" .. player.UserId in your PlayerRemoving connection. This is likely causing your data to disappear!

Line 54 would be

if success then
		print("SUCCESS!!")
		PersonalBests.Level1.Value = data <-- This line, data is always nil
	else...
1 Like

bruh, I’ve been looking at this script for hours and hours… how did I overlook that :clown_face:

It’s all good! We all make mistakes like that occasionally so don’t worry! :grinning:
If I helped out then feel free to mark my solution and the post as solved :slight_smile:

You are a very cool dude, and yeah, i did

1 Like