Is this how you do a renderstepped loop?

the cooldown is only for the performance of the client and server, cooldowns are also server sided, so dont worry for any security vulnerabilities, i was wondering if this is the correct way to do a renderstepped loop for sending a remote while mouse button 1 is down, and if there is a better way to script this.

local COOLDOWN_TIME = 0.35
local lastFireTime = 0

local function onRenderStepped(step)
	-- Check if mousebutton1 is held down
	if UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
		local currentTime = tick()
		if currentTime - lastFireTime >= COOLDOWN_TIME then 
			CombatEvent:FireServer()
			print("sent")
			-- Fire CombatEvent here
			lastFireTime = currentTime
		end
	end
	runService.RenderStepped:Wait()
end

runService.RenderStepped:Connect(onRenderStepped)
1 Like

you can do it like this, however i do suggest removing this part as a renderstep loop is not a regular loop, such as while and for. RenderStepped calls the onRenderStepped function every frame, so adding the wait doesn’t really do anything, as a renderstep loop cant be exhausted.

But with a render stepped loop being fired every frame you might want to reconsider using a different loop, such as UIS.InputChanged, fires every time a key “updates”, it continuously fires if a key is held. Another alternative would be something like this

local mouse = game.Players.LocalPlayer:GetMouse()
local MB1 = false
mouse.MouseButton1Down(function()
    MB1 = true
    while (MB1) do
        --logic
        task.wait()
    end
end)

mouse.MouseButton1Up(function() MB1 = false end)
1 Like

The code you provided would technically work, but checking whether or not the left mouse button is held down on every frame is usually considered inefficient. It would be best practice to rely on event-based logic so that the RenderStepped loop is only active when necessary. By listening to mouse input changes, we can connect and disconnect the loop only when the state of the mouse button changes:

local COOLDOWN_TIME = 0.35
local lastFireTime = 0

local onRenderSteppedConnection --initial connection variable

local function onRenderStepped(step)
	local currentTime = tick()
	if currentTime - lastFireTime >= COOLDOWN_TIME then
		CombatEvent:FireServer()
		print("sent")
		-- Fire CombatEvent here
		lastFireTime = currentTime
	end
end

local function handleMouseInput(input, state) --function that checks the provided input
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if onRenderSteppedConnection then -- disconnect existing function
			onRenderSteppedConnection:Disconnect()
			onRenderSteppedConnection = nil
		end
		if state then
			onRenderSteppedConnection = runService.RenderStepped:Connect(onRenderStepped) -- connect new renderstepped function	
		end
	end
end

UIS.InputBegan:Connect(function(input, gameprocessedevent)
	if gameprocessedevent then return end
	handleMouseInput(input, true)
end)

UIS.InputEnded:Connect(function(input, gameprocessedevent)
	if gameprocessedevent then return end
	handleMouseInput(input, false)
end)
1 Like