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

How to make a function wait until game is loaded?

Asked by 6 years ago

Hello! So I have two separate leaderboard stats that I cannot manage to integrate into one so I decided to do two different scripts but a problem I have is that when the scripts load in a playing session, sometimes one has not loaded fully and the script wont be able to detect it and the script will break. How can I fix this or make my two leaderboards one?

local DSService = game:GetService('DataStoreService'):GetDataStore('Subscribers')
game.Players.PlayerAdded:connect(function(plr)
    -- Define variables
    local uniquekey = 'id-'..plr.userId
    local leaderstats = plr.leaderstats
    local savevalue =  Instance.new('IntValue')

    savevalue.Parent = leaderstats
    savevalue.Name = 'Subscribers'

    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        savevalue.Value = GetSaved[1]
    else
        local NumbersForSaving = {savevalue.Value}
        DSService:SetAsync(uniquekey, NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.leaderstats.Subscribers.Value}
DSService:SetAsync(uniquekey, Savetable)        

end)

Answer this question