How would I go about lowering script activity from function that runs 300 times every milesecond

I want to lower the activity to below 0.5% if possible.

local function UpdateDistance(i, CameraLocation)
	distanceX = math_abs(GrassFolder[i].Position.X - CameraLocation.X)
	distanceY = math_abs(GrassFolder[i].Position.Y - CameraLocation.Y)
	distanceZ = math_abs(GrassFolder[i].Position.Z - CameraLocation.Z)

	distance = round((distanceX + distanceY + distanceZ)/5)
	return distance, TickRate[i], VelChangeRateX, VelChangeRateZ

I’ve tried adding tick rate so it runs the code less often, however it still doesn’t fix the main issue as it runs at 0.7% script activity rather then the 4% before adding tick rate. The delay also doesn’t help the effect I’m trying to make using the distance value.

I’m running it on RenderStepped and the function is being called from a for loop.

It appears you’re trying to change the camera location. You only need it to run once a frame. Also you appear to be running a square root on a square?

The square root on a square is to remove the negative without adding an if statement to do it.

That’s what math.abs is for…


Wasn’t aware of that, will change it quickly.

I would connect that function to render stepped:

Put this line under the function and remove what ever ran it many times before:


UpdateDistance(feed your data here)


This will only work if your on the client. You can change it to “Heartbeat” if it’s on the server.

I’m already using it on RenderStepped, within a for loop. Will quickly add that to the post.

Why is it in a for loop? If there it’s not lagging then there is no issue.

I’m grabbing all the grass from a table using the for loop.

for i = 1, GrassAmount, 1 do
	UpdateDistance(i, CameraLocation)

If there is a much easier and less expensive way in doing this I’m not sure what it is. This also grabs multiple other table values, not only the grasses position.

What exactly are you trying to do? Are you trying to change the camera location?

No, I’m trying to calculate the distance between the grass selected in the for loop and the player’s camera.

What are you using that number for?

Every 5 distance away the grass animation moves slower to drop script activity and once distance is over 40 the grass is moved to a folder in lighting so it can stop affecting script activity.

Client sided activity will only count towards the player’s hardware, so if someone’s computer is not that great performance on client sided scripts will not be that good as well, but games run on a tick loop. Take unity for example, from my experience most things run through the Update function which runs every frame, and as long as you don’t have very complex calculations or very expensive code running every frame, you are most likely fine. I had a similar issue with my first person animated movement which was updated every frame, tested it in different machines and everything was fine.
I do however love the fact you’re adding such optimizations to your game, very unique and not seen that often

1 Like