My game keeps crashing after I run a script

Hi, I am pretty new to scripting, and am just dipping my toes in the advanced part of scripting, and every time I run the local script where when a player pressed “R” the script adds 32 to a starting 16 value, and stop after it gets to the number 330, but as I ran the script, my game started to crash, is there a better way of doing this, or am I just dumb?

–The Script—
local UserService = game:GetService(“UserInputService”)

local r = 16

local rkeypressed = false

UserService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.R then
print(“player has pressed down the R key”)
rkeypressed = true
while rkeypressed == true do
wait()
r = r + 32
print(r)

		while r > 330 do
			r = r + 32
		end
	end
end

end)

UserService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
print(“R is being pressed”)
rkeypressed = false
end
end)

Replace

while r > 330 do
	r = r + 32
end

with

while r < 330 do
    r += 32
end

That wouldn’t work, I am trying to make it, so it stops at 330.

Try the code instead of automatically assuming it doesnt work. The code you wrote only runs after you get to 330, and loops infinitely. The code I provided loops UNTIL it gets to it, then it stops

I did try it, it keeps going beyond 330

Never mind, I figured out the issue, I didn’t use the wait function so it went mega fast and was my PC couldn’t keep up

honestly im just gonna edit your whole script, it has issues…

local UserService = game:GetService(“UserInputService”)

local r = 16

UserService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.R then
        if r+32 <= 330 then
            r+=32
        end
    end
end)

based on your description this code makes more sense. This code will, every time R is pressed, add 32 to R, until it can be no larger without going over 330