I don't understand anymore. I've followed the DataStore tutorial up and down, and now on usage attempt of ChangeStat, I get the following error:

  20:56:15.159  ServerScriptService.DatastoreModule:54: attempt to concatenate string with nil  -  Server  -  DatastoreModule:54

  20:56:15.159  Stack Begin  -  Studio

  20:56:15.159  Script 'ServerScriptService.DatastoreModule', Line 54 - function ChangeStat  -  Studio  -  DatastoreModule:54

  20:56:15.159  Script 'Workspace.Levels.4.SpawnLocation.IncreaseLevel', Line 15  -  Studio  -  IncreaseLevel:15

  20:56:15.159  Stack End  -  Studio

ChangeStat:

function PlayerDataHandler:ChangeStat(player, statName, value)
	local playerUserId = "Player_" .. player.UserId
	assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
	if typeof(sessionData[playerUserId][statName]) == "number" then
		sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
	else
		sessionData[playerUserId][statName] = value
	end
	player.leaderstats.Level.Value = sessionData[playerUserId]["Level"]
	player.leaderstats.Tokens.Value = sessionData[playerUserId]["Tokens"]
end

Touched ChangeStat call:

local PlayerDataHandler = require(game.ServerScriptService:WaitForChild("DatastoreModule"))

local tokAmt = 1

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player ~= nil then
		local playerLvl = player.leaderstats.Level.Value
		local playerTok = player.leaderstats.Tokens.Value
			if playerLvl == tonumber(script.Parent.Parent.Name) then
				--do noTHINGGGG
				return
			else if playerLvl == ((tonumber(script.Parent.Parent.Name)) - 1) then
				PlayerDataHandler.ChangeStat(player, "Level", (tonumber(script.Parent.Parent.Name) + 1))
				PlayerDataHandler.ChangeStat(player, "Tokens", (player.leaderstats.Tokens.Value + 1))
			end
		end
	end

end)

3 comments

0

Somewhere in your script you are attempting to combine a string value with a nil value, (ex. "text" .. nil). So it doesnt have anything to do with datastores. Not quite sure where its occuring, since there is code removed.

0

Printing the player value inside of ChangeStat results in a part name being printed, which isnt accessed anywhere in the actual call of the function. Printing player right before the call of the function prints the correct player object. The attempted concatenation is inside of ChangeStat, (local playerUserId = "Player_"..player.UserId).

0

the player parameter being passed through ChangeStat is being interpreted as a Part object inside the ModuleScript somehow. The error is related to datastores because its a code piece pulled directly from the dev wiki.