Which is best for performance? RenderStepped or loops? (or anything else I haven't mentioned)

As the title says: what has been proven to be faster, and less performance heavy? The RenderStepped event, or a loop? Or are they both equal, or are there better alternatives? Thanks.

loops will run constantly at a variable speed… renderstepped only works on the client and fires each frame.

1 Like

So which would be less taxing on performance / FPS?

what types of loops are you doing? loops only become a problem when they run thousands of times a second.

I’m currently using while loops that wait 0.5, and for each run, it checks if the mouse is on a specific target, and if it is, it tweens a GUI. The only issue is that if you hover onto the target, it subtracts around 20 FPS (for me at least). I thought it might’ve been the fact it was a loop.

Well you can do this. I don’t think a loop with 0.5 wait should lag you. Try this:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

mouse.Move:Connect(function()

print("mouse moved")

end)

Oh hey, it works, but only when I’m stood still or only making minor movements. If I’m continually moving at full speed at the same time, it still lags.

hmm. that cant be the loop or my script then. send your script over and i can see if there is a memory leak or something very inefficient.

Are you making like a gun? Since im not sure why you need a loop for this.

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local rs = game:GetService("RunService")
local uis = game:GetService("UserInputService")

local pickUpPart = Instance.new("Part", character)
pickUpPart.Position = Vector3.new(character.Torso.Position.X, character.Torso.Position.Y, character.Torso.Position.Z + 3)
pickUpPart.Size = Vector3.new(.1,.1,.1)
pickUpPart.CanCollide = false
pickUpPart.Transparency = 1

local pickUpPartWeld = Instance.new("Weld", pickUpPart)
pickUpPartWeld.Part0 = character.Torso
pickUpPartWeld.Part1 = pickUpPart
pickUpPartWeld.C1 = CFrame.new(0,-1,2)

local isPickingUp = false
local canPickUp = false
local debounce = false

local function onMouseMove()
	if mouse.Target ~= nil then
		if mouse.Target:GetAttribute("CanBePickedUp") ~= nil then
			if character.HumanoidRootPart.Position.Magnitude - mouse.Target.Position.Magnitude <= 5 then
				game.Players.LocalPlayer.PlayerGui.Prompts.PickUp:TweenPosition(UDim2.fromScale(.45, .75), "Out", "Linear", 0.15, true, nil)
				canPickUp = true
			end
		else
			canPickUp = false
			game.Players.LocalPlayer.PlayerGui.Prompts.PickUp:TweenPosition(UDim2.fromScale(.45, 1.2), "Out", "Linear", 0.15, true, nil)
		end
	end
end

mouse.Move:connect(onMouseMove)

Note that the local function isn’t complete yet, so if it feels like something is missing, you know why.

So you are trying to put something that says if they can pick it up?

Since instead of doing that just use the prompt and put the maxdistance to whatever you want it to be.

So how could I do that? Perhaps give an example?

im assuming that your function is being fired so fast and often it will lag. Here is a script that should add a 0.5 cool down.

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local rs = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local debounce = true

local pickUpPart = Instance.new("Part", character)
pickUpPart.Position = Vector3.new(character.Torso.Position.X, character.Torso.Position.Y, character.Torso.Position.Z + 3)
pickUpPart.Size = Vector3.new(.1,.1,.1)
pickUpPart.CanCollide = false
pickUpPart.Transparency = 1

local pickUpPartWeld = Instance.new("Weld", pickUpPart)
pickUpPartWeld.Part0 = character.Torso
pickUpPartWeld.Part1 = pickUpPart
pickUpPartWeld.C1 = CFrame.new(0,-1,2)

local isPickingUp = false
local canPickUp = false
local debounce = false

local function onMouseMove()
	if mouse.Target ~= nil then
		if mouse.Target:GetAttribute("CanBePickedUp") ~= nil then
			if character.HumanoidRootPart.Position.Magnitude - mouse.Target.Position.Magnitude <= 5 then
				game.Players.LocalPlayer.PlayerGui.Prompts.PickUp:TweenPosition(UDim2.fromScale(.45, .75), "Out", "Linear", 0.15, true, nil)
				canPickUp = true
			end
		else
			canPickUp = false
			game.Players.LocalPlayer.PlayerGui.Prompts.PickUp:TweenPosition(UDim2.fromScale(.45, 1.2), "Out", "Linear", 0.15, true, nil)
		end
	end
end

mouse.Move:Connect(function()
	if debounce then
		debounce = false
		onMouseMove()
		wait(0.5)
		debounce = true
	end
end)

Assuming ur using proximity Prompts it should work like this: Once you set the MaxAcitivationDistance to whatever number once you go farther than that distance it will disappear and there would be no need to check where the mouse is pointing at.

While that may be a good method, my preferences defiantly prefer on-screen GUIs. (also, since my game is first person, and said item is not too big, it might get in the way, AND (2nd edit) this system is meant to a be a modular way of picking up anything with a certain attribute. With proximity prompts I’d have to do everything slowly and inidvidually.)

Also, It’s not made too much of a difference whenever I move quickly.

If you want to test how often the mouse move event is firing, you can print something on RenderStepped or Heartbeat, and print in your event connection. Those RunService events run at 60 FPS so you can tell if your event is firing faster than the frame rate. Though I don’t really see this being the issue with the denounce idea - does this still happen if you disable the script?

By the looks of it, it may be the function itself. I’ve tried binding to RenderStepped, and using a debounce, with no luck. I put it in a loop with a 1 second wait, and when I move quickly, every time that wait ended, I saw a FPS dip. So defo something with the function.