So I used this Documentation - Roblox Creator Hub for my player and I had help to add in an extra function for gettin data which is this
function PlayerStatManager:GetData(player, statName)
local playerUserId = "Player_" .. player.UserId
assert(sessionData[playerUserId], "GetData error: Data for " .. player.Name .. " does not exist.") --if data does not exist for player, then error
if statName then
local value = sessionData[playerUserId][statName]
assert(value, "GetData error: Value does not exist for: ".. statName) --if there is no data for specified stat, then error
return value -- if we specified a specific stat, then return the data for that stat
return sessionData[playerUserId] --if we didn't specify a specify stat, then return all the player's data
However something I wanna know is if using assert is like actually helpful or useless? Because in one of my scripts I’m trying to get the players data or like wait for it to load, by doing
while not playerData:GetData(player) do wait(1) end
However it seems like having the assert is always gonna error although I’m honestly not sure how to properly wait for the players data before getting it so like there’s probably a way to do it lol.
I think the asserts are okay here. You can make use of pcall to make sure that your loop doesn’t get broken by the GetData call erroring.
For example i think this would work:
local data = nil
local success = false
while not success do
success, data = pcall( function() return playerData:GetData(player) end)
if not success then
-- If the pcall fails the data variable will contain a the error message from the function in string form
-- Warn doesn't break loops and is basically just a print but with a different coloured text
Edit: One issue with this code however is that it will retry indefinitely so I think an important improvement may be to have a way of checking how many times it’s tried to retrieve the data and stop trying past some number of attempt.
Hmmm would I replace this with my while loop I have? or would I like put this inside my module?
In this case, I would not use assert. There is no point to make your scripts error when you are just waiting for a value. If you just remove the
assert statements you would get the same result since the
while not PlayerStatManager:GetData(player, statName do wait(1) end
will keep on getting a nil value until the data loads.
One thing to note is that this could lead to an infinite loop if the player’s data never loads. I would recommend to only call
GetData when you need the data, not to just wait for it to load.
well I’m trying to wait for the data to load because I’m trying to like load in some of the players stuff an some some things to the client altho this might not be good, but just basically what I’m doing.
I assume you are using data stores for this, so what you would do to avoid the busy loop is:
--Get the data via a data store
--Then do whatever loading you want to do
Yeah I wrote it with the intention that it could replace the while loop that you had in your post.