Will this RenderStepped FireEvent cause lag at all?

This is my local script:

local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer
local mouse = plr:GetMouse()
local rs = game:GetService("RunService")

local old = script.Parent:WaitForChild("Target").Value

while true do
	rs.RenderStepped:Wait()
	script.Parent.TargetChanged:FireServer(mouse.Hit.p)
end

And my server script:

script.Parent.TargetChanged.OnServerEvent:Connect(function(plr,pos)
	script.Parent.Target.Value = pos
end)

Will this cause any lag if the server has 6-8 people each?

dont wait for renderstepped, just connect to it
game:GetService(“RunService”).RenderStepped:Connect(function()

end)
but, if done on the client, likely not.

1 Like

Most likely it will, because it is being fired every frame and a Vector3 is sent over, which would probably take a lot of kilobytes, and would be likely to surpass the 50KB/s limit where bandwidth occurs.

What’s the use of this? Why are you updating it every frame? This is not really the practical way to go

2 Likes

I don’t know a practical way to go, if I have a script on my server, that requires my mouse position but I need it after a 2 second delay. (and I need the newest mouse position)

You could add a RemoteEvent in ReplicatedStorage that gets the mouse.Hit. Fire it whenever you need it (e.g. every 2 seconds) and you’re good to go.

1 Like

If you only need it every 2 seconds then you don’t need to fire the Remote every frame - that’s just inefficient. You can use a while loop, with a 2-second wait, then you fire the RemoteEvent:

local last = tick()
while true do
    local now = tick()
    if (now - last) >= 2 then
         last = tick()
         -- fire the event
    end
    rs.RenderStepped:Wait()
end

I feel like it’d be better to just use wait(2).

while true do
  -- Fire the event
  wait(2) -- 2 second delay
end
2 Likes

It looks like a placement script, correct? Instead of firing the location of the part every frame, fire it once it is done placing. (if it’s not a placement system, tell me what it is)

Frankly, I haven’t considered that frame rates can drop randomly which causes it to be fired less frequently, and it’s necessary to utilize RenderStepped, so wait is the better alternative

No, but it’s going to delay your frames. Don’t use RenderStepped if you aren’t updating the character or the camera. The correct event to be using here should be Heartbeat and it should be after the remote is fired, not before - yielding at the beginning of an iteration is bad practice.

In general, you should not be firing events like this. Why do you need to have this fired frequently? Realistically you would only need to fire this when a change that others need to see becomes apparent.

4 Likes

What type of system are you making anyways?

Really depends on what you’re doing listening to the RemoteEvent on the server. Also, I wouldn’t recommend using RenderStepped for stuff like that.

I wanted to make a “Charging weapon” and send the location of my mouse to the server constantly for the ray, but there isn’t really a need to do it so fast, but you see.

Then, I would probably think RunService.Heartbeat, because you are sending a RemoteEvent to the server with the mouse. Remember this, RunService.Stepped should be used when you need to do things before the physics update, RunService.RenderStepped should be used for the character or camera, otherwise, RunService.Heartbeat should be used. However, the code you are using won’t do much lag, because you aren’t making a connection, but you are waiting for the event to be fired instead. You should also use mouse.Hit.Position instead, as CFrame.p is deprecated, in favour of CFrame.Position