Help use DeltaTime In RunService

hello, i have a Idle Camera movement that uses RunService and it works fine in studio but in game it gets sped up, i tried doing what worked for other people but cant get it, any help?

Original Script

local speedx,x,speedy,y = 76,0.75,62,0.75 
local lspeed = 0.01 
local function lerp(s,e,a) return s+(e-s)*a end
local ix = 0
local iy = 0
local pi,sin,pi2 = math.pi,math.sin,math.pi*2
local cam = workspace.CurrentCamera
local lsx,lx,lsy,ly = speedx/1000,x/1000,speedy/1000,y/1000

game:GetService("RunService"):BindToRenderStep("Swaying",201,function()
	if game.Players.LocalPlayer.Character:WaitForChild("Humanoid").MoveDirection.Magnitude == 0 then -- we are idle
	lsx=lerp(lsx,speedx/1000,lspeed)
	lx=lerp(lx,x/1000,lspeed)
	lsy=lerp(lsy,speedy/1000,lspeed)
	ly=lerp(ly,y/1000,lspeed)
	ix = (ix + lsx)%pi2
	iy = (iy + lsy)%pi2
		cam.CFrame = cam.CFrame * CFrame.Angles(sin(ix)*lx,sin(iy)*ly,0)
	end
end)

Mine

local speedx,x,speedy,y = 76,0.75,62,0.75 
local lspeed = 0.01 
local function lerp(s,e,a) return s+(e-s)*a end
local ix = 0
local iy = 0
local pi,sin,pi2 = math.pi,math.sin,math.pi*2
local cam = workspace.CurrentCamera
local lsx,lx,lsy,ly = speedx/1000,x/1000,speedy/1000,y/1000

game:GetService("RunService"):BindToRenderStep("Swaying",201,function(deltaTime)
	deltaTime = deltaTime * 60
	if game.Players.LocalPlayer.Character:WaitForChild("Humanoid").MoveDirection.Magnitude == 0 then -- we are idle
		lsx=lerp(lsx,speedx/1000,lspeed* deltaTime)
		lx=lerp(lx,x/1000,lspeed* deltaTime)
		lsy=lerp(lsy,speedy/1000,lspeed* deltaTime)
		ly=lerp(ly,y/1000,lspeed* deltaTime)
	ix = (ix + lsx)%pi2
	iy = (iy + lsy)%pi2
		cam.CFrame = cam.CFrame * CFrame.Angles(sin(ix)*lx,sin(iy)*ly * deltaTime, 0)
	end
end)

Could you provide a short clip of what this looks like ingame?

1 Like

had some videos in this post

okay, tell me what you mean by it gets sped up.

1 Like

u can see in the video that the idle camera movement acts 2x faster in game than studio which is caused smth with the fps

1 Like

let me problem solve for a bit, I’ll be back soon.

1 Like

Okay I believe that I have a little script, see if this works for you…

local speedx, x, speedy, y = 76, 0.75, 62, 0.75 
local lspeed = 0.01 
local function lerp(s, e, a) return s + (e - s) * a end
local ix, iy = 0, 0
local pi, sin, pi2 = math.pi, math.sin, math.pi * 2
local cam = workspace.CurrentCamera
local lsx, lx, lsy, ly = speedx / 1000, x / 1000, speedy / 1000, y / 1000

game:GetService("RunService"):BindToRenderStep("Swaying", 201, function(deltaTime)
    -- deltaTime ensures frame rate independence
    if game.Players.LocalPlayer.Character:WaitForChild("Humanoid").MoveDirection.Magnitude == 0 then -- we are idle
        -- Apply deltaTime scaling to account for different frame rates
        lsx = lerp(lsx, speedx / 1000, lspeed * deltaTime)
        lx = lerp(lx, x / 1000, lspeed * deltaTime)
        lsy = lerp(lsy, speedy / 1000, lspeed * deltaTime)
        ly = lerp(ly, y / 1000, lspeed * deltaTime)
        
        -- Increment ix and iy based on time passed
        ix = (ix + lsx * deltaTime) % pi2
        iy = (iy + lsy * deltaTime) % pi2
        
        -- Apply camera sway using sin and the calculated angles
        cam.CFrame = cam.CFrame * CFrame.Angles(sin(ix) * lx, sin(iy) * ly, 0)
    end
end)

I changed this a bit so that it runs at the same rate regardless of the framerate.
I also updated the CFrame.Angles bit to make it smoother

Try this out and report back. Sir you there?

Okay I won’t be able to talk until next weekend, hope this works.

1 Like

my bad i barley use dev forum so i dont check up alot , i tested it and the camera seems to just keep moving to the top left of my screen slowly

1 Like