Rewriting LocalScript That is Using RunService to reduce lag

I have a LocalScript that essentially checks an Attribute in a Player to see if it’s true or false, and based on that will decide whether or not to create/enable a Proximity Prompt on eligible players in the game. My issue is that I think this may be a cause of lag when large amounts of players are in my game. How can I rewrite this so that it retains the same function but doesn’t create the insane amount of lag? Thanks!

Here’s my code:

if Prompts then
	RunService:BindToRenderStep("TippingPrompts", 0, function()
		local CurrentPlayers = Players:GetChildren()

		for _, Player in ipairs(CurrentPlayers) do
			if Player:GetRankInGroup(GroupID) >= StaffRank and Player ~= LocalPlayer then
				local Character = Player.Character
				if Character ~= nil then
					local ProximityPrompt = Character.HumanoidRootPart:FindFirstChild("TippingPrompt")
					if ProximityPrompt then
						if Player:GetAttribute("TipsEnabled") then
							ProximityPrompt.Enabled = true
						else
							ProximityPrompt.Enabled = false
						end
					else
						ProximityPrompt = Instance.new("ProximityPrompt")
						ProximityPrompt.Name = "TippingPrompt"

						ProximityPrompt.RequiresLineOfSight = false
						ProximityPrompt.ObjectText = "Tip"
						ProximityPrompt.ActionText = Player.Name
						ProximityPrompt.MaxActivationDistance = 10

						ProximityPrompt.Parent = Character.HumanoidRootPart

						ProximityPrompt.Triggered:Connect(function()
							if Hub.Visible == false and Prompt.Visible == false then
								openPrompt(Player.UserId)
							end
						end)

						if Player:GetAttribute("TipsEnabled") then
							ProximityPrompt.Enabled = true
						else
							ProximityPrompt.Enabled = false
						end
					end
				end
			end
		end
	end)
end

you can maybe offload it to the client since you say it is laggy when there are large amounts of player . The client and tell the server that the player’s rank in the group js high enough . The server would receive the message and verify

Why do you need to check every player every frame (like 60 times per second or so)? I don’t get why you used Renderstep.

This script is going to check every player over and over again, 40-60 times per second endlessly. Is that what you want it to do?

1 Like

To be honest with you, I don’t get why Renderstep was used either. This is a script that was purchased from another scripter by my client and recently the game has been dealing with a lot of lag so I figured this was a possible cause. I was just wondering how I could rewrite this so that there wouldn’t be so much unnecessary checking but still gets the job done. Would you have any ideas by any chance?

Maybe just listen for the “TipsEnabled” attribute to change.

Here is how to listen for an Attribute Change:

That way you do not need to run an endless loop to listen. You respond when it changes then toggle the ProximityPrompt. Or, something like that.

This was my initial thought, but when I tried this it simply broke the entire system. No errors in the output, it just simply wouldn’t create the ProximityPrompts. I tried moving things around and doing some reordering to no avail. When I tried adding print statements, nothing would print either. Would you have any suggestions on how to incorporate this without it breaking? Thanks!

How often to the ranks change? If it is rare you could check once. If somewhat frequent you could use a while loop and task.wait(someduration).

If you don’t have any other property that you could :connect

Some other ideas, :connect to characteradded(once per reset) or playeradded(once per join) to do the check.

Simple but ‘sloppy’? fix:

replace:

RunService:BindToRenderStep("TippingPrompts", 0, function()
...
end)

with:

while true do
     task.wait(1) --every one second do:
...
end

A few questions:

  1. Do you need to know if the RankInGroup changes during game play?

  2. Is there a reason you are not adding the “TippingPrompt” when they join the game?

If you could explain WHY this script was written it might make it easier for other people to help you re-work it.