"attempt to preform arithmetic (div) on number and nil"

For context: I’m trying to make a game/script where the player can change their fps.

The error is in the title–attempt to preform arithmetic (div) on number and nil.

This is my code:

local gui = script.Parent
local textBox = gui.Frame.TextBox


local fps = tonumber(textBox.Text)
local clock = tick()

while true do 
	if fps == nil then
		fps = 60
	else
		fps = tonumber(textBox.Text)
	end
	
	while clock + 1/fps > tick() do end
	wait()
	clock = tick()
end

It’s saying that it can’t do division on a number and nil, but it should be turning whatever the player types into the TextBox into a number with tonumber(). And if it’s empty then the default should be 60 fps.
The error runs first thing, before I type anything into the TextBox. And when I do type nothing happens.

I want to know what I’m doing wrong and how this can be fixed.

2 Likes
  1. There’s no need to be calling tonumber twice when it’s done at the top of the script.
  2. Please replace the wait() with RunService.RenderStepped:Wait()
  3. Can you try printing what the original fps value is?

There is though, because otherwise it won’t update each loop. He just has it formatted backwards.
It should read

    local fps = tonumber(textBox.Text) or 60
    while clock + 1/fps > tick() do end -- busy waiting
    wait()
    clock = tick()

use waitforchild when getting textbox

gui:WaitForChild(“Frame”).TextBox

should work to replace gui.Frame.TextBox

Enjoy!

Edit: after second look, its possible that another issue may be the cause. it would help if you gave more context on the error

Woah I thought this didn’t post correctly but I guess it did! I’ll try out the replies now.

By combining all the replies, it works now! Thank you all!