Log in to vote

How to load a function after GetAsync?

Asked by 9 days ago
Edited 7 days ago

Detailed explanation here. I created a character customization menu and i stored the data on Server Storage. And then i created a function to load those each time the character respawns, which is player.Character.CharacterAdded. It works. The thing is, this script runs before the getAsync could get any data from the server so the first time a player joined. They will not load their customised character.

Now i temporarily fixed this by waiting around (5) to (10) after a character has been added, and then loading it. It works, but it isn't exactly giving my game a polished feel. Because it also affects the player respawn. Their custom char wont load until 5-10 sec.

Full script over here:

(sorry if you cringe while reading the script. It's only been a few days since i started roblox studio with no experience in scripting at all so bear with me :> )

Alright. What should i do to make it run the load function after only after the data has been loaded from the server.

Any help would be greatly appreciated.

Can you show the full script? The error is located at line 36 but you didn't provide any of lines 36. If you did send the script, please tell us what line its starting at Xapelize 1080 — 9d
You may need to share the full script for different reasons - Single blocks of scripts have linear evaluation, and the CharacterAdded is only bound after the info is loaded. I fail to see how this could be resolved by a wait. eLunate 5136 — 9d
Line 36 is just the function to load the player's color selection, which on first time joining will results in nil becuase the function has been loaded before th server could fetch the player's customization save. But sure, let me share the whole script. LoyalQuickAngel 14 — 7d

1 answer

Log in to vote
Answered by
Yuuwa0519 197
9 days ago
Edited 9 days ago

I think a module made by evaera called "Promise", a library ported from Java? (not too sure), which is a future-proofing method may be handy here. in fact, the examples they provide uses a http service get request, which is similar to datastore get request, so it may be very useful for you :D

--Example usage of promise with datastore, if you are confused with http example

local DSS_NAME = "PlayerDataV1"
local DATA_PREFIX = "_DataV1"

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

--Modules / Libraries
local Promise = require( --i'd recommend having it in replicated storage

local PlrDataStore = DSS:GetDataStore(DSS_NAME)

local dataGet(key)
    return Promise.async(function(resolve, reject)
        local success, result = pcall(PlrDataStore, PlrDataStore, key)

        if success then

local function LoadData(plr)
    dataGet(plr.Name .. DATA_PREFIX)
            --andThen() only runs when resolve() is called in dataGet()
            --do the plr.CharacterData. stuff here

            return body --returning body allows the next andThen() to read the body 
            --promise allows you to chain stuff, so you can do multiple stuff
            --catch() is only ran when reject() is called in dataGet()
            --the data wasn't loaded, so either kick a player or retry the load data.
            --Finally is ran when the other calls above are all done running whether it successes or fails.
            print("Done Doing the Process of Promise!!")


Yes, its very complicated, but trust me the Promise is one of the most useful library as roblox involves many asynchronous calls (mainly data store, other like remote functions, etc)

Alright i'll look into it, thank you. I'll mark this as an answer as soon as i get it to work :) LoyalQuickAngel 14 — 7d

Answer this question