This has been an issue for months now, I honestly don't know what to do anymore. Whenever someone joins a server lasting for a few minutes, the chat lags and everyone starts walking in place. It starts in small spikes, that only last a few seconds. However, they slowly grow into massive lag after 5 or so more join. Here is my main suspect behind the lag:

--------------------
--| WaitForChild |--
--------------------

-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
	assert(parent, "ERROR: WaitForChild: parent is nil")
	while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
	return parent[childName]
end

-----------------
--| Variables |--
-----------------

local GamePassService = Game:GetService('GamePassService')
local PlayersService = Game:GetService('Players')
local InsertService = Game:GetService('InsertService')
local LightingService = Game:GetService('Lighting') --TODO: Use new data store service once that exists

local GamePassIdObject = WaitForChild(script, 'GamePassId')
local ToolAssetsToLoad = WaitForChild(script, 'ToolAssetsToLoad')

local AdminTools = LightingService:FindFirstChild('AdminTools')

-----------------
--| Functions |--
-----------------

-- Makes copies of all the admin tools and puts them in target
local function CloneAdminTools(target)
	for _, tool in pairs(AdminTools:GetChildren()) do
		local toolClone = tool:Clone()
		toolClone.Parent = target
		game.Workspace.InsertBoard.Parent = target
	end
end

-- When a player with the game pass joins, give them the admin tools
local function OnPlayerAdded(player)
	if GamePassService:PlayerHasPass(player, GamePassIdObject.Value) then
		local starterGear = WaitForChild(player, 'StarterGear')
		CloneAdminTools(starterGear)

		if player.Character then -- They've already loaded and won't get their StarterGear until next spawn
			local backpack = WaitForChild(player, 'Backpack')
			CloneAdminTools(backpack)

	end
end
end

	


-- Create AdminTools if it doesn't exist
if not AdminTools then
	AdminTools = Instance.new('Model')
	AdminTools.Name = 'AdminTools'

	-- Load all of the assets in ToolAssetsToLoad and put them in AdminTools
	for _, intObject in pairs(ToolAssetsToLoad:GetChildren()) do
		if intObject and intObject:IsA('IntValue') and intObject.Value then
			local assetModel = InsertService:LoadAsset(intObject.Value)
			if assetModel then
				local asset = assetModel:GetChildren()[1]
				if asset then
					asset.Parent = AdminTools
				end
			end
		end
	end

	AdminTools.Parent = LightingService
end

PlayersService.PlayerAdded:connect(OnPlayerAdded)

^^^ That is a script that gives the player their tools bought from a gamepass.

Thank you for reading, and attempting to help. Any help is appreciated! Thank you :D Roblox: happyhal75

Discord: happyhal75#2503

Thanks :D

4 comments

0

i send you a friend request accept it please

0

Well optimize it go check on the roblox wiki how to optimize your game

0

Why are you reinventing WaitForChild and not using ReplicatedStorage?

0

Open up Script Performance, and show a screenshot, of what you see (should be located in View tab)