[UnSolved] Setting Player.Character Lags out the game

  1. What do you want to achieve?
    I would like for when the player holds alt and clicks an NPC, they can switch into the NPC with no lag.

  2. What is the issue?

Every Time A Player Switches To An NPC, The Lag Gets Worse Every Time.

  1. What solutions have you tried so far?
    I’ve looked around and I have not seen anyone with this issue before.

ServerScriptService: (Server Script)

Event.OnServerEvent:Connect(function(Player,Data1)
	if Data1.Name == "NPC" or Data1.Name == Player.Name then
		Player.Character.Archivable = true
		local Clone = Player.Character:Clone()
		Player.Character = Data1
		Player.Character:FindFirstChild("Animate").Enabled = false
		task.wait(0.01)
		Player.Character:FindFirstChild("Animate").Enabled = true
		Clone.Parent = game.Workspace
		print(Clone)
		Event:FireClient(Player,Data1)
	end
end)

StarterPlayerScripts: (Local Script)

local Event = game.ReplicatedStorage:WaitForChild("PlayerControlEvent")
local UIS = game:GetService("UserInputService")
local Fire = false
local Mouse = game.Players.LocalPlayer:GetMouse()

UIS.InputBegan:Connect(function(Input,GPE)
	if GPE == false then
		if Input.KeyCode == Enum.KeyCode.LeftAlt then
			Fire = true
		end
	end
end)

UIS.InputEnded:Connect(function(Input,GPE)
	if GPE == false then
		if Input.KeyCode == Enum.KeyCode.LeftAlt then
			Fire = false
		end
	end
end)

Mouse.Button1Down:Connect(function()
	local Thing = Mouse.Target
	if Thing ~= nil and Fire == true then
		if Thing.Parent.Name == "NPC" or Thing.Parent.Name == game.Players.LocalPlayer.Name then
			local Data1 = Thing.Parent
			Event:FireServer(Data1)
		end
	end
end)

Event.OnClientEvent:Connect(function(CData1)
	game.Workspace.CurrentCamera.CameraSubject = CData1
end)

The more you switch characters, the more your game lags. It may not lag at first but keep changing characters and it gets worse and worse over time. (Game freezing for like half a second is fine, but anything more than a second is not acceptable to me.)

1 Like

I suggest using microprofiler or opening the script performance window in the view tab to identify the problem by yourself if no one has given their answer yet

Yea I’ve tried doing that, I’ve seen no spike on anything though.