How can I make my script work with an FPS unlocker?

Hey, I have a script that bobs your camera as you move, and as expected, it quite literally has a seizure when you play with an FPS unlocker.
Is there any way I can modify my code to fix this issue?

Code for the bobbing script
while true do
	wait();
	if game.Players.LocalPlayer.Character then
		break;
	end;
end;
camera = game.Workspace.CurrentCamera;
character = game.Players.LocalPlayer.Character;
Z = 0;
damping = character.Humanoid.WalkSpeed / 2;
PI = 3.1415926;
tick = PI / 2;
running = false;
strafing = false;
character.Humanoid.Strafing:connect(function(p1)
	strafing = p1;
end);
character.Humanoid.Jumping:connect(function()
	running = false;
end);
character.Humanoid.Swimming:connect(function()
	running = false;
end);
character.Humanoid.Running:connect(function(p2)
	if p2 > 0.1 then
		running = true;
		return;
	end;
	running = false;
end);
character.Humanoid.Died:connect(function()
	running = false;
end);
function mix(p3, p4, p5)
	return p4 + (p3 - p4) * p5;
end;
while true do
	game:GetService("RunService").RenderStepped:wait();
	fps = (camera.CoordinateFrame.p - character.Head.Position).Magnitude;
	if fps < 0.52 then
		Z = 0;
	else
		Z = 0;
	end;
	if running == true and strafing == false then
		tick = tick + character.Humanoid.WalkSpeed / 102;
	else
		if tick > 0 and tick < PI / 2 then
			tick = mix(tick, PI / 2, 0.9);
		end;
		if PI / 2 < tick and tick < PI then
			tick = mix(tick, PI / 2, 0.9);
		end;
		if PI < tick and tick < PI * 1.5 then
			tick = mix(tick, PI * 1.5, 0.9);
		end;
		if PI * 1.5 < tick and tick < PI * 2 then
			tick = mix(tick, PI * 1.5, 0.9);
		end;
	end;
	if PI * 2 <= tick then
		tick = 0;
	end;
	camera.CoordinateFrame = camera.CoordinateFrame * CFrame.new(math.cos(tick) / damping, math.sin(tick * 2) / (damping * 2), Z) * CFrame.Angles(0, 0, math.sin(tick - PI * 1.5) / (damping * 20));
end;

any help is appreciated, thank you in advance

1 Like

Replace this with a wait (either empty or a small decimal).

2 Likes

same name! :smile:

though, replacing this line with a wait() didn’t quite do anything, as a matter of fact, it broke the whole script and the camera won’t move at all now.

Alrighty, then revert that back :flushed:

I am going to do some experimentation and get back to you soon.

1 Like

much appreciated, thank you very much!

I am unfamiliar with the use of FPS unlockers. Can you send me the one you are using?

1 Like

sure thing, here’s the one i use (and i’m 90% sure everyone else does too)

What do you mean by a seizure? Please be more descriptive.

1 Like

Hey there. Still experimenting and working on your issue.

FPS unlockers increase the FPS of the game. The animation is based on the FPS not the time, so it speeds up the animation a bunch.


@OP

I’d see if changing these lines:

game:GetService("RunService").RenderStepped:wait();
-- ...
tick = tick + character.Humanoid.WalkSpeed / 102;

to

local step = game:GetService("RunService").RenderStepped:wait();
-- ...
tick = tick + character.Humanoid.WalkSpeed / 102 * (30*step);

solves the problem.

There is the dampening part at the end, that math can get a little messy converting that into something time based.

1 Like

I have revised the code for you, limiting the FPS of the bobbing at 40 (neutral).

while true do
	wait();
	if game.Players.LocalPlayer.Character then
		break;
	end;
end;
camera = game.Workspace.CurrentCamera;
character = game.Players.LocalPlayer.Character;
Z = 0;
damping = character:WaitForChild("Humanoid").WalkSpeed / 2;
PI = 3.1415926;
tick = PI / 2;
frcheck = 0
ideal = 0.01
running = false;
strafing = false;
character.Humanoid.Strafing:connect(function(p1)
	strafing = p1;
end);
character.Humanoid.Jumping:connect(function()
	running = false;
end);
character.Humanoid.Swimming:connect(function()
	running = false;
end);
character.Humanoid.Running:connect(function(p2)
	if p2 > 0.1 then
		running = true;
		return;
	end;
	running = false;
end);
character.Humanoid.Died:connect(function()
	running = false;
end);
function mix(p3, p4, p5)
	return p4 + (p3 - p4) * p5;
end;
game:GetService("RunService").RenderStepped:Connect(function(d)
	frcheck+=d
	if frcheck>=ideal then 
		frcheck=0
if running == true and strafing == false then
		tick = tick + character.Humanoid.WalkSpeed / 102;
	else
		if tick > 0 and tick < PI / 2 then
			tick = mix(tick, PI / 2, 0.9);
		end;
		if PI / 2 < tick and tick < PI then
			tick = mix(tick, PI / 2, 0.9);
		end;
		if PI < tick and tick < PI * 1.5 then
			tick = mix(tick, PI * 1.5, 0.9);
		end;
		if PI * 1.5 < tick and tick < PI * 2 then
			tick = mix(tick, PI * 1.5, 0.9);
		end;
	end;
	if PI * 2 <= tick then
		tick = 0;
	end;
	camera.CoordinateFrame = camera.CoordinateFrame * CFrame.new(math.cos(tick) / damping, math.sin(tick * 2) / (damping * 2), Z) * CFrame.Angles(0, 0, math.sin(tick - PI * 1.5) / (damping * 20));
	end
end)

Out of curiosity, how does this code work? I see the new waited, frcheck, and d variables, but it looks like all of them don’t do anything besides update each other, so I don’t think they would have any effect on the code.

Edit:

Oh okay.

i tried this, and it also ended up breaking the code. not sure what’s going on

edit: it did something, but it didn’t do what it was supposed to, it offset the camera to the right and broke it

Hey there. I made an edit to my response, my apologies.

Bind it to render step and use delta time. Right now you are waiting for Renderstepped in a while loop, but you can use that to call a function instead of as just a wait and it passes in the amount of time since the last time it was called which you can multiply by values in your math to affect timing.

1 Like

someone else told me to use delta time as well, how can i?

I posted this above, this is using delta time. You can also create a connection instead of using a loop.

(The step variable is the change in time, if you base your change in camera movement based on the change in time then the FPS doesn’t affect the rate of the camera manipulation)

@Foxxive Did it one more time :flushed: I tested with your FPS unlocker and had successful results.

Yeah, I forgot to embed the rest of the code in an if statement. I also changed ideal to 0.01 as 0.07 is too high. Basically it waits until the delta adds up to above the ideal time, then resets it.

1 Like

So in your code you are currently doing this

while true do
    game:GetService("RunService").Renderstepped:Wait()
end

You can do this instead

local function everyframe(deltaTime)
    --Your code here.  deltaTime is the time since it ran last.
end
game:GetService("RunService"):BindToRenderStep("everyframe", Enum.RenderPriority.Camera.Value + 1, everyframe) --Runs the function right after the camera updates on every frame
1 Like