ServerScriptService.GameHandler:61: attempt to index nil with number

idk why but the datastore does not work when i go into the client and change it then return to the main client and then leave. When i rejoin nothing comes up as an error

	local Folder = Instance.new("Folder")
	Folder.Parent = Player
	Folder.Name = "leaderstats"

	local Wins = Instance.new("IntValue")
	Wins.Parent = Folder
	Wins.Name = "Wins"
	Wins.Value = 0
	local Coins = Instance.new("IntValue")
	Coins.Parent = Folder
	Coins.Name = "Coins"
	Coins.Value = 0
	
	local playerUserId = "Player_"..Player.UserId


	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(playerUserId)
	end)

	if success then
		Wins.Value = data[1]
		Coins.Value = data[2]
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local Wins = player.leaderstats.Wins
	local Coins = player.leaderstats.Coins

	local data = {Wins.Value, Coins.Value}

	local success, errormessage = pcall(function()
		DataStore:SetAsync(playerUserId, data)
	end)

	if success then
		print("Data successfully saved!")
		if data then
			print("Data collected!")
			-- data is a table

			Wins.Value = data[1]
			Coins.Value = data[2]

		else
			-- no data
			--probably the players first time playing

		end
	else
		print("There was an error!")
		warn(errormessage)

	end

end)```

Try to test it in-game and not in studio!

1 Like

The Server does not Recognize Client Changes, as they do not Replicate, A Client Change will only effect the Players View of the game, with a few exceptions (Like Movement, and Animations).

Also:

local data
local success, errormessage = pcall(function()
    data = DataStore:GetAsync(playerUserId)
end)

data = data or TemplateData -- if the Player does not have Data, it will give them Starter Data

the script does not locate any TemplateData?

whenever i click the error it goes to this line: Wins.Value = data[1]

You cannot handle datastore on the client. It must be done on the server.

-- instead of doing this 
if success then
    Wins.Value = data[1]
    Coins.Value = data[2]
end

-- you should do this 
if data and success then
    Wins.Value = data[1]
    Coins.value = data[2]
else
   warn(errormessage)
end