Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I fix lag whenever people join my game?

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)

3 comments

0

Do you use Free Models (models got from Toolbox)? May be there is an malicious code at one of it.

0

You should not seperate the DataStore scripts. Keep the saver and leaderstats maker together.

0

Ok! Will try that! :)

1 answer

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.

4 comments

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.

0

Another note, if your game is higher than 10,000 parts, it will cause lag spikes

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..

0

Hmm, epic.

This discussion is preserved as it was. New answers, comments, and votes are closed.