Higher FPS Causes Function to Finish Faster Than Those With a Lower FPS

Hello everyone, I have a function that is supposed to play a camera shaking effect when it is called, and the camera shakes how I want it to, however if a person has a higher fps, their function and camera-shake effect will finish faster than someone with a lower fps, effectively giving the person with a higher fps an advantage over the one with a lower fps.

I tested this by using Alt-Enter & an FPS-Unlocker, and found that when I have a higher fps (>60), the function finishes much faster than intended, while when I have an FPS of 60 or lower, the function runs for the correct duration. Code is below. Any help is greatly appreciated!

local function EffectForLaser()
	local count = 1
	laserConnection = RunService.RenderStepped:Connect(function(fps)
		if count < 800 and laserConnection ~= nil then
			local a = math.random(-10, 30)/100
			local b = math.random(-10, 30)/100
			local c = math.random(-10, 30)/100
			HUMANOID.CameraOffset = Vector3.new(a,b,c)
			count += 1
			task.wait(fps)
		else
			laserConnection:Disconnect()
			laserConnection = nil
			HUMANOID.CameraOffset = Vector3.new(0, 0, 0)
			print("LaserConnection set to nil and humanoid offset set to default")
		end
	end)
end

The RunService.RenderStepped function returns one parameter which is DeltaTime, or the amount of time elapsed since the event last ran.

Rather than using task.wait(), we should instead have a set time that is desired between each time we offset the camera, such as:
local TIME_TO_WAIT = .1
laserConnection = RunService.Heartbeat:Connect(function(Delta_Time)
local ADJUSTED_WAIT_TIME = TIME_TO_WAIT * (Delta_Time * 60)
task.wait(ADJUSTED_WAIT_TIME)
end)

Hey. Thanks for the suggestion, but it did not seem to fix the issue. The function still finishes faster when I have a higher FPS. Wondering if it has to do with the count variable, but I’m not sure how else I could end the effect once I need it to end. I was thinking about using a boolean somehow, but I’m not sure how to implement that into the function in its current state. Any ideas?

local function EffectForLaser()
	local count = 1
	laserConnection = RunService.RenderStepped:Connect(function(fps)
		if count < 800 and laserConnection ~= nil then
			local a = math.random(-10, 30)/100
			local b = math.random(-10, 30)/100
			local c = math.random(-10, 30)/100
			HUMANOID.CameraOffset = Vector3.new(a,b,c)
			count += 1
			local timeToWait = 0.1*fps*60 -- Change made here
			print(timeToWait)
			task.wait(timeToWait)
		else
			laserConnection:Disconnect()
			laserConnection = nil
			HUMANOID.CameraOffset = Vector3.new(0, 0, 0)
			print("LaserConnection set to nil and humanoid offset set to default")
		end
	end)
end

It should be count += fps * 60. The wait statement does nothing.

However, the camera will shake faster anyways so…

1 Like

Sorry for the late reply I’ve been pretty busy. This works perfectly! Thanks!

1 Like

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