Hello! I'm testing out a few module scripts to try and better understand how they work. I went through the steps on the guide for this one: https://developer.roblox.com/articles/Saving-Player-Data This is what I had issues with when I types it out:
function PlayerStatManager:ChangeStat(player, statName, value)
assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
local playerUserId = "Player_" .. player.UserId
if typeof(sessionData[playerUserId][statName]) == "number" then
sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
else
sessionData[playerUserId][statName] = value
end
end
I really don't understand this part, and I keep getting the ChangeStat error or other errors when I do it:
assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
Can someone help me understand what this is for? Is it needed? If so what is it used for and what might I be doing wrong? If I just remove that line, it seems to work - the value changes just fine. Thank you!

4 comments
That line allows you to verify the type of value being received by the script is the same type of value that it will be changing. https://developer.roblox.com/articles/Built-in-Functions-and-Variables/Roblox ..so a string wont overwrite a number. This is very usful in securing your datastores, Never trust the client. In the first block of code make sure the "playeruserid" variable is at the top
of the function, right now line two is trying to reference something that has not been set yet"playeruserid". So its returning nil instead of the value from the table.
guess at this point I should have just written a full answer....oh well
Thank you!! I origanlly tried that (since it seemed odd that it was in that order) but it didn't work at the time so I changed it back. I noticed something else I did wrong, changed that, but forgot to change this too. Also the explanation for why its needed is really helpful! Thanks!!!