Script laggy on client

Hey everyone,

I made a script to rotate the player in the \ | / directions. After a lot of struggling, I finally got it to work. But when I run the game, it looks really smooth on the server, but on the client, it looks bad and laggy.

Does anyone know why this happens?

Thanks!

Heres Script

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.PrimaryPart = character:FindFirstChild("Right Leg")

		if character:FindFirstChild("Animate") then
			character.Animate:Destroy()
		end

		local humanoid = character:WaitForChild("Humanoid")
		local hrp = character:WaitForChild("HumanoidRootPart")
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0
		humanoid.JumpHeight = 0
		
		humanoid:ChangeState(Enum.HumanoidStateType.Physics)

		task.wait(0.1)

		local base = character:GetPivot()
		local t = 0
		local speed = 3.7

		game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
			t = t + deltaTime * speed
			local angle = 45 * math.cos(t)
			local newCFrame = base * CFrame.Angles(math.rad(angle), 0, 0)
			if character.PrimaryPart.CFrame ~= newCFrame then
				character:PivotTo(newCFrame)
			end
		end)
	end)
end)

Heres Client Video

And Heres Server Video

you need to put the code on the client because there’s a delay between the server and client

1 Like

you mean firing remote to server?

or just copy the code onto a local script (get rid of the PlayerAdded event)

alr that kinda worked but now its bit laggy on server

nobody’s playing on the server

yeah but other people see through server?

alr man you fixed problem so thanks

Hey there. Just a quick note: Whatever happens to the client’s character is automatically replicated to the server, and therefore replicated to the other clients. Hope that helps.

1 Like