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

How do I fix lag whenever people join my game?

Asked by 5 years ago

I have 2 scripts that could be causing this (Code will be at the end.) The first script is the one that creates the leaderboard. The second script is the one that saves the players' data. It is most likely the PlayerEntered or PlayerAdded functions causing this. (I am bad at both.) Any help would be appreciated, thanks!

Roblox account: Happyhal75

First script (Leaderboard)

game.Players.PlayerAdded:connect(function(p)
    local stats = Instance.new("IntValue")
    stats.Name = "leaderstats"
    stats.Parent = p

    local money = Instance.new("IntValue")
    money.Name = "Obby Wins" -- Change "Money" to anything you want to name it like "Cash"
    money.Parent = stats
end)

Second script: (Saver)

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("GemSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
    local folder = Instance.new("Folder", plr)
    folder.Name = "leaderstats"
    local gems = Instance.new("IntValue", folder)
    gems.Name = "Obby Wins"


    gems.Value = ds1:GetAsync(plr.UserId) or 0
    ds1:SetAsync(plr.UserId, gems.Value)


    gems.Changed:connect(function()
        ds1:SetAsync(plr.UserId, gems.Value)
    end)


    end)

0
Do you use Free Models (models got from Toolbox)? May be there is an malicious code at one of it. cailir 284 — 5y
0
You should not seperate the DataStore scripts. Keep the saver and leaderstats maker together. Frometa1998 35 — 5y
0
Ok! Will try that! :) happyhal75 32 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Well, I don't see any problem with the two scripts. Although maybe cailir is right? You must check if you have any malicious scripts, that is if you did use any free models.

0
I used no free models though, the only one I am using is a dodgeball. However, it seems pretty clear. Also, lately it has been getting kind of better, as I disabled reseting. happyhal75 32 — 5y
0
Another note, if your game is higher than 10,000 parts, it will cause lag spikes Simpletton 82 — 5y
0
I put them all into one script, and it helped, but didn't completely terminate the issue. It really only happens when 2 or more people join at the same time or around the same time. I honestly don't know what to do now.. happyhal75 32 — 5y
0
Hmm, epic. Simpletton 82 — 5y
Ad

Answer this question