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:


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()
1 Like

You should look into RunService, it’s more smoother than task.wait(), if that’s what your using.

2 Likes

it is using runservice tho but it still lag

2 Likes

It’s probably due to priorities. Use :BindToRenderStep and try changing the priority to before and after the camera.

RunService:BindToRenderStep("action", Enum.RenderPriority.Camera.value - 1, function()
    -- your script here
end)
RunService:BindToRenderStep("action", Enum.RenderPriority.Camera.value + 1, function()
    -- your script here
end)
1 Like

nah it doesnt work idk why :skull: :skull:

also, i changed the post a bit

1 Like