Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

(Answered) Testing ModuleScripts: What am I doing wrong? Why can it not find the Player.UserId?

Asked by
Songist 49
5 years ago
Edited 5 years ago

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!

1
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 ForeverBrown 356 — 5y
1
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. ForeverBrown 356 — 5y
1
guess at this point I should have just written a full answer....oh well ForeverBrown 356 — 5y
0
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!!! Songist 49 — 5y

1 answer

Log in to vote
0
Answered by
Necro_las 412 Moderation Voter
4 years ago
Edited 4 years ago

Hey, i am learning to work with data stores too, right now, and im also using this article as base, but i am getting an error in the "setupPlayerData" funtion when it tries to get

local data = playerData:GetAsync(playerUserId)

the error says

 502: API Services rejected request with error. HTTP 403 (Forbidden)

It is very mysterious to me why the fuck this is happening. If it happens to you too pls advise me so i'm sure it is not my code. Else I looked about this error and it only use to happen when API services or HTTP requests are disabled or when the dev is on teamcreate, and that's none of them my case. I read that sometimes this errors show up for some time randomly, but im affraid it never goes out cause it is like this since yesterday.

Pls feedback me if you are getting this error too (if you already scripted that part)!

And about your question, looks like it is already answered, the purpose of that code is to check if the value you are saving in the table is of the same type than the value that is already there (number, string, object, etc), so it will prevent you from making something like:

sessionData["ColorOfHead"] = sessionData["ColorOfHead"] + 1

-- and get an error

-- so if is not a number, it will update the data like this:

sessionData["ColorOfHead"] = "String"
Ad

Answer this question