Why does part lag when player changed direction

when the player move forward, the part moves smoothly but when the player turns around, it starts lagging during the turn

game file: Tilt-UI new.rbxl (51.6 KB)

the main script is in starter gui and here is script:

note: you dont need to download file, just copy this script and put it in a local script and put the script in startergui


local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer

local TweenService = game:GetService("TweenService")

local function create3DInterface()
	-- Wait for the player's character to load
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

	-- Check if "stats" or "interface" parts already exist
	if character:FindFirstChild("stats") or character:FindFirstChild("interface") then
		warn("3D interface parts already exist. Skipping creation.")
		return
	end

	-- Create the stats part
	local statsPart = Instance.new("Part")
	statsPart.Size = Vector3.new(7.54, 6.777, 0.49)
	statsPart.Anchored = true
	statsPart.CanCollide = false
	statsPart.BrickColor = BrickColor.new("Bright blue")
	statsPart.Transparency = 0.4
	statsPart.Name = "stats"
	statsPart.Parent = game.Workspace

	-- Create the interface part
	local interfacePart = Instance.new("Part")
	interfacePart.Size = Vector3.new(5.766, 4.569, 0.49)
	interfacePart.Anchored = true
	interfacePart.CanCollide = false
	interfacePart.BrickColor = BrickColor.new("Really red")
	interfacePart.Transparency = 0.4
	interfacePart.Name = "interface"
	interfacePart.Parent = game.Workspace


	-- Offset for interface part
	local interfaceOffset = CFrame.new(0, 0.6, -2.5) * CFrame.Angles(0, math.rad(180), 0)
	local statsOffset = CFrame.new(0, -2, -0.5) * CFrame.Angles(math.rad(110), 0, math.rad(180))
	-- Shaking effect for both parts
	local shakeAmplitude = 0.1
	local shakeSpeed = 2
	local RunService = game:GetService("RunService")

	local connection
	connection = RunService.RenderStepped:Connect(function(deltaTime)
		if humanoidRootPart and humanoidRootPart.Parent then
			local time = tick() * shakeSpeed
			local shakeX = math.sin(time) * shakeAmplitude
			local shakeY = math.cos(time) * shakeAmplitude
			local shakingOffset = CFrame.new(shakeX, shakeY, 0)

			-- Directly update the parts to match HumanoidRootPart
			statsPart.CFrame = humanoidRootPart.CFrame * statsOffset * shakingOffset
			interfacePart.CFrame = humanoidRootPart.CFrame * interfaceOffset * shakingOffset
		else
			connection:Disconnect()
		end
	end)
end

create3DInterface()

the problem is that the runservice connection isnt updating at the right times

try using

RunService:BindToRenderStep("whatever name", Enum.RenderPriority.Camera.Value - 1, function(deltaTime)

if that doesnt work then maybe use a weld or spring

edit: also try Enum.RenderPriority.Character since the thing is bound to the character

1 Like

where do i put this part though???

replace the runservice connection you already have

it doesnt work :skull: for some reason idk why

It has to do with the order of render/physics.

Try use Heartbeat instead, it should fix it.

1 Like

i tried it but it does not work. it still lags when turned

I knew I had fixed this before!
The issue is that the hrp doesnt rotate every renderstepped, only per heartbeat and those two are unsynced.

Solve it by setting the HRP cframe every renderstepped

local look = workspace.CurrentCamera.CFrame.lookVector
humanoidRootPart.CFrame = CFrame.new(Vector3.new(0,0,0), Vector3.new(look.x, 0, look.z)) + humanoidRootPart.Position
			
-- Directly update the parts to match HumanoidRootPart
statsPart.CFrame = humanoidRootPart.CFrame * statsOffset * shakingOffset
interfacePart.CFrame = humanoidRootPart.CFrame * interfaceOffset * shakingOffset
1 Like

THANKS SO MUCH! I spent a long time trying to fix this and it works!

thanks to all the people who helped in this post :grinning:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.