If players join my game too quickly (like 10 people trying to join at once), sometimes the player's data refuses to load in and then the server is laggy.

Here is my Data Storage script:

local DSService = game:GetService('DataStoreService'):GetDataStore('ls3DataStorage')
ID = 6 
SaveTime = 300 
leaderboardname = "Stats" 

StatList = {Beli = 500, Strength = 0, XP = 0, Stamina = 100, MaxStamina = 100, Level = 1, StatPoints = 0, Blade = 0, DevilFruit = 0, LogiaValue = 0, StaminaLevel = 1, Defense = 0, Pirate = 1, Marine = 0, Quest = 0, KilledBandits = 0, GrassGot = 0, KurosKilled = 0, SSJBanditsKilled = 0, DevilFruitLevel = 0, CurrentSword = 0, Jump = 0, SoruEXP = 0, CurrentStyle = 0, Did5Kuro = 0, HakiGuysKilled = 0, YEETCHEST = 0, Buso = 0, BusoPoints = 0, NightClonesKilled = 0, CurrentDualSword = 0, HasRhino = 0, HasFire = 0, Spawn = 0 }




function CreateValue(Name, Parent, Value) 
    Value = Value or 0
    local savevalue =  Instance.new('IntValue', Parent)
    savevalue.Name = Name
    savevalue.Value = Value
    return savevalue
end

function GetValue(uniquekey)

    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        return GetSaved
    end
end

game.Players.PlayerAdded:connect(function(plr)
    local uniquekey = ID..plr.userId
    local Levelss = Instance.new('IntValue', plr)
    Levelss.Name = leaderboardname
    for i,v in pairs(StatList) do
        local Stat = GetValue(i..uniquekey)
        if Stat then
            CreateValue(i, Levelss, Stat)
        else
            CreateValue(i, Levelss, v) 
        end
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local uniquekey = ID..plr.userId
    if plr:FindFirstChild(leaderboardname) then
        for i,v in pairs(StatList) do
            if plr[leaderboardname]:FindFirstChild(i) then
                DSService:SetAsync(i..uniquekey, plr[leaderboardname][i].Value)
            end
        end
    end
end)

while wait(SaveTime) do
    P = game:GetService("Players"):GetChildren()
    for o=1, #P do
        if P[o] then
            local uniquekey = ID..P[o].userId
            if P[o]:FindFirstChild(leaderboardname) then
                for i,v in pairs(StatList) do
                    if P[o][leaderboardname]:FindFirstChild(i) then
                        DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value)
                    end
                end
            end
        end
    end
end

When the Player's data refuses to load in, they have to rejoin until it loads correctly, and then things like shockwave effects and what not appear and fade away slower than they should. The whole server is laggy until it shuts down basically. Is there anyway around this without having to use something like DataStore2?

Thanks for reading

2 comments

0

Bad script. Non-descriptive variable names and stolen from alvinbloxx

0

The variables are descriptive, and it isn't stolen from anyone. Why can't I downvote a comment?