How should i make sure if my datastore loaded?

Something i’d like to say off the bat is that the datastore was made for singleplayer games, so it makes my life a lot easier. the way i’d check if a value loaded was put whatever code i needed to execute inside a function with an if statement, call the function and then use the .Changed event, which worked but this adds up in memory and is probably not the best way to do this. then i thought “maybe adding a value to workspace and setting it to true after the LoadData() function finishes” which is literally the same thing pretty much. i then thought to just add a value to workspace and use WaitForChild which will ensure that if i need the data a few seconds after the player joins i won’t have issues with checking if the value is true and all of that, but it looks really weird in code and i also have to put math.huge so it doesn’t throw a warning. i went with a bindable event but that will also be bad if i need the data later so, what should i do? right now i’m thinking i should go with the WaitForChild method. code snippet isn’t really needed here since i’m just asking how to check if the data loaded from another script, so everything works i just wanna know the best method for this.

One method for checking if the data has loaded from a DataStore is to use a combination of a boolean value and a RemoteEvent. After the data has been successfully loaded on the server, set a boolean value (e.g., “PlayerDataLoaded”) to true for that specific player. Then, use a RemoteEvent to notify the client that the data has been loaded.

-- Server Script
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local dataLoadedEvent = Instance.new("RemoteEvent")
dataLoadedEvent.Name = "DataLoadedEvent"
dataLoadedEvent.Parent = ReplicatedStorage

local function loadPlayerData(player)
    local success, data = pcall(function()
        return playerDataStore:GetAsync(player.UserId)
    end)
    
    if success and data then
        -- Apply loaded data to the player
        -- ...

        -- Set a value in the player to indicate data is loaded
        local dataLoaded = Instance.new("BoolValue")
        dataLoaded.Name = "DataLoaded"
        dataLoaded.Value = true
        dataLoaded.Parent = player
        
        -- Notify the client that data has been loaded
        dataLoadedEvent:FireClient(player)
    else
        warn("Failed to load data for player: " .. player.Name)
    end
end

Players.PlayerAdded:Connect(loadPlayerData)

-- Client Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local dataLoadedEvent = ReplicatedStorage:WaitForChild("DataLoadedEvent")

local function onDataLoaded()
    print("Player data has been loaded!")
    -- Perform any necessary client-side actions here
end

dataLoadedEvent.OnClientEvent:Connect(onDataLoaded)

-- If you need to wait for the data in another script:
local function waitForDataLoaded()
    if not player:FindFirstChild("DataLoaded") then
        player:WaitForChild("DataLoaded")
    end
    return true
end

-- Usage:
local dataIsLoaded = waitForDataLoaded()
if dataIsLoaded then
    -- Proceed with game logic that requires loaded data
end
1 Like

Well most of the data checking happens on the server, and using a boolean just brings me to the run function then connect to Changed event dilemma which probably isn’t too good for memory

1 Like