Is it possible to cap FPS?

where did the “fps” come from?

Is this from a renderstepped script or something

1 Like

Just declare fps in the beginning of the script.
Edit: I have modified the script.

1 Like

Aight I understand, cool

30 charrr

1 Like

I made a script, copy pasted your script, and placed it in ServerScriptStorage.

Hey, just tested it out. It works for me in Studio but not in game, I’m still clueless why.

1 Like

ServerStorage is for storing items not needed to be replicated to clients

2 Likes

In a studio test the client and server ends are running on the same machine (yours). In an online game that script would only be slowing down the server. It needs to slow down the client. So @OptimisticSide’s code should be put in a local script in StarterPlayerScripts.

2 Likes

Did as you said and I tested it out.

The script lowered the FPS count but did not really cap it to the number listed.

Ex. I set the fps variable to 10, when I went into my place with my FPS unlocker my FPS was still around 130 FPS
When I had the variable set to 60 I was around 180 FPS
When I had the script disabled I was around 270 FPS

1 Like

You aren’t supposed to cap the FPS if it gives you an advantage. You’re supposed to write code that works with all framerates.

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function(deltaTime)
    -- multiply the movement amount by deltaTime.
end)

You most likely forgot to multiply the movement by deltaTime before you applied it on the player. Delta time is basically the time it took to render the last frame, and it’s useful in creating smooth animations on any framerate, and making sure things tied to renderstep happen at the same speed on all clients, no matter what their framerate is.

In the unlikely even that it’s in a roblox script, you can just clone the roblox scripts while you’re testing and then paste it into studio when you’re done testing. Then, find the code that relates to swimming and make sure they are multiplying the movement by deltaTime. If they aren’t, you can edit the script to fix it, and then file a bug report.

Capping FPS is going to make a lot of players confused/annoyed, and they’re probably going to leave the game because of it. Instead of finding a hacky solution like capping FPS, you should just fix the actual issue.

There’s a helpful devforum article on it here:

13 Likes

I know this conversation happened a few months ago, but I was trying to do the same thing and the script didn’t work. It capped fps, but not at the specified number. I don’t know why this is. Ex: Specified number: 10 Fps: 22

1 Like

This is a better script:

local RunService = game:GetService("RunService")
local MaxFPS = 30
while true do
    local t0 = tick()
    RunService.Heartbeat:Wait()
    repeat until (t0 + 1/MaxFPS) < tick()
end

Note this won’t be exact (normally within 5 fps, lower fps values will be closer to the desired FPS) but is closer than what was posted. Also, unless this is for an effect (like Super Nostalgia Zone’s 30 fps cap to emulate old roblox), code should be designed to work on any framerate, so just keep that in mind :slightly_smiling_face:

19 Likes

Thanks for the help. I just tested it and it capped at exactly 30.

1 Like

its not but i think you can kick players manually by counting the fps

local RS = game:GetService("RunService")
local FPS = 0
local player = game.Players.LocalPlayer
RS.RenderStepped:Connect(function()
FPS += 1
end)
while task.wait(1) do
if FPS > 63 then
player:Kick()
else
FPS = 0
end
end

you need to make the fps count a little bit more higher than 60 because sometimes roblox exceeds that limit
(just realized im super late😅 sorry for that)

2 Likes

I know it may be unimportant, but reorganizing your code with the proper tabs/spaces will help newer people understand what it’s doing a lot faster

1 Like

You can probably fix the speed issue by dividing it by the time passed using os.clock()

I don’t want to be kicked just because I used an FPS unlocker, and maybe Roblox will use VSync in future instead of a 60 FPS cap, making this code kick everyone who has a monitor with a higher refresh rate.

3 Likes

After a bit of research i found this script

local RunService = game:GetService("RunService")
local MaxFPS = 30
while true do
    local t0 = tick()
    RunService.RenderSteppedt:Wait()
    repeat until (t0 + 1/MaxFPS) < tick()
end

i didnt make it but it works just fine.
edit : just realized this was posted here before.

My god this is horrible, the script activity is over 70% sometimes.

5 Likes

Sorry I’m late:

Could you explain this? I don’t understand what tick() does, or why we set clock to tick() again?

Returns how much time has elapsed, in seconds, since the UNIX epoch, on the current local session’s computer. The UNIX epoch is represented by the date January 1st, 1970.

1 Like

for anyone looking for a better solution, this caps it at exactly the cap you put, even if they are using an fps unlocker or anything else it will still work

local fpsCap = 165
local clock = tick()

game:GetService("RunService").RenderStepped:Connect(function()
	while clock + 1 / fpsCap > tick() do end
	clock = tick()
	
	task.wait()
end)

image

9 Likes