Game lagging after player picks gender

Hello there! My friend and I created a game that works, but the lag gets really bad after a while and the server completely shuts down for all players (for some reason). The lag seems to start at the beginning after you choose your character’s gender and doesn’t stop from there (we used humanoid description if that helps). Is there any ideas on why this is is happening and/or how we can fix this?

Here is a video of the issue: (look at the mouse carefully since it may be hard to see the lag)

The ‘Client Memory Usage’ is at 884 MB if that helps at all.

What code is there? How are things being handled?
It’s hard to remove lag if we have no idea what may actually cause it.

2 Likes

I have absolutely no idea what is causing it exactly either, but I have a few ideas:

  • Humanoid Description
  • The data that loads the player’s bought characters when they join back
  • The amount of remote events/functions we are using (16 total)

Here is some code that could possibly help:

local function onPlayerJoin(Player)
	Player.CharacterAdded:Connect(function(char)
		char.Humanoid.WalkSpeed = 0
		char.Humanoid.JumpPower = 0
		wait(0.35)
		Remotes.PickGender.OnServerEvent:Connect(function(Player, Char)
			if Char == "Male" then
			DefaultCharGender = "MaleDefault"
			elseif Char == "Female" then
				DefaultCharGender = "FemaleDefault"
			end
			if DefaultCharGender == "MaleDefault" then
			Player.Character.Humanoid:ApplyDescription(script.MaleDefault)
			elseif DefaultCharGender == "FemaleDefault" then
				Player.Character.Humanoid:ApplyDescription(script.FemaleDefault)
			end
		end)
		char:SetPrimaryPartCFrame(workspace.Spawn.CFrame)
		local CurrentChatWith = Instance.new("StringValue",Player)
		CurrentChatWith.Name = "CurrentChatWith"
	end)
	Player.Chatted:Connect(function(msg)
			if string.sub(msg,1,5) == "/kick" then
				local target = game.Players:FindFirstChild(string.sub(msg,7))
				if target then
					target:Kick("You have been kicked by an admin.")
				end
			elseif string.sub(msg,1,9) == "/announce" then
				local Message = string.sub(msg,11)
				for i,v in pairs(game.Players:GetPlayers()) do
					v:FindFirstChild("PlayerGui").Gui.AnnounceSend.Visible = true
					v:FindFirstChild("PlayerGui").Gui.AnnounceSend.Text.Text = game:GetService("Chat"):FilterStringForBroadcast(Message, Player)
				end
				wait(7)	
				for i,v in pairs(game.Players:GetPlayers()) do
					v:FindFirstChild("PlayerGui").Gui.AnnounceSend.Visible = false
					v:FindFirstChild("PlayerGui").Gui.AnnounceSend.Text.Text = ""
				end
					
			end
  		end)
	
	ReplicatedStorage.Data.PlayerNumVal.Value = ReplicatedStorage.Data.PlayerNumVal.Value + 1
	
	local Bought = Instance.new("Folder")
	Bought.Name = "Bought"
	Bought.Parent = Player
	
	local Coins = Instance.new("IntValue")
	Coins.Parent = Player
	Coins.Name = "Coins"
	
	local Equipped = Instance.new("StringValue")
	Equipped.Parent = Player
	Equipped.Value = "None"
	Equipped.Name = "Equipped"
	
	local GetCharacters = DataStore.GetData(Player, PlayerStore, "Characters")
	if GetCharacters then
		for i,v in pairs(GetCharacters) do
			local New = Instance.new("IntValue")
			New.Name = v
			New.Parent = Player.Bought
			Remotes.UpdateButtons:FireClient(Player, v)
			print(Player.Name.." has "..v)
			Player.PlayerGui:WaitForChild("Gui").Store.Scrolling[v].Price.Text = "OWNED"
		end
	end
	
	
	PlayerStorage[Player.Name] = {Chat = "None", Available = false}	
	
	
	
	local CoinsSaved = DataStore.GetData(Player, PlayerStore, "Coins") or 0
	Coins.Value = CoinsSaved
	
	local function playerCountTime()
		local TimePassed = 0
		while true do
			DataStore.SetData(Player, PlayerStore, "Coins", Coins.Value)
			wait(50)
			TimePassed = TimePassed + 1

			Coins.Value = Coins.Value + 5
		end
	end
	
	spawn(playerCountTime)
end
Players.PlayerAdded:Connect(onPlayerJoin)