Ways for multiple player location checks without polling

I am working on a dialogue system and I am stuck on how I want to check if a player is near a npc and to popup the prompt key. I seen some that just use a table with all of the npcs and iterate through all of the locations and check the distance. Others just use a box around each npc and when one is hit it just triggers a remote event.
Is there any other way to check a players position other than a loop, and how would you go about this?
Any help is appreciated!
Thanks.

1 Like

You could just use a BillboardGui with text such as, “press e to open chat”, and have it only be visible when the player is a certain distance from it, using the MaxDistance property of BillboardGui’s.

Then to actually pull up the dialog you could check the distance between the player character and the CPU when “e” is pressed.

1 Like

I guess GetPropertChangedSignal on the HumanoidRootPart of the player works.

RootPart:GetPropertyChangedSignal("Position"):Connect(function()
   -- magnitude check
end)

This would not stop him from having to look through all the CPUs when this happens though. You would still need to use a loop to check the distance between you and the dialog CPUs in order to determine how close you are, and bring up a the “press e” GUI if you are within a certain proximity of a CPU.

He is trying to avoid looping every time the players position changes.

I currently use this to do the position checking and then to fade in the gui prompt [press e]

while wait() do
	local Character = Player.Characteracter
	local RootPart = Character:FindFirstChild("HumanoidRootPart")
	if Character and RootPart then
		local CurrentNPC = nil
		local Distance = 7
		for i,NPC in pairs(NPCS) do
			if (NPC.Position - RootPart.Position).Magnitude < Distance then
				CurrentNPC = NPC
			end
		end
		if CurrentNPC and debounce == false then
			PromptAdd(CurrentNPC)
			NPC = CurrentNPC
		elseif CurrentNPC == nil or debounce == true then
			PromptRemove(CurrentNPC)
			NPC = nil
		end
	end	
end

I handle the opening and start of the dialouge by checking for e and if npc ~= nil, Like @ExcessEnergy said.
But when I have 50 NPCs the script starts to go over 1% in the script performance.
Its not that much, but for some mobile devices I think it might slow down the game, in combination of other scripts.
@Kampfkarren made a post about Avoiding wait() and why, and I want to follow along with that.
I got the usage of the script down a lot by adding a wait(0.1) in both loops but it just doesn’t seem like a good way to go at it. Im not sure if there is any other way to do this.

That wouldn’t work, try running it in Studio. That event only fires when the Position is manually set by a Script, not when physics or whatever move a part (it would be firing so much that it’d be essentially useless if that wasn’t the case).

2 Likes

A BillboardGui has a option to only pop up when a player is near, so you would not have to run a ludicrous loop.
Just use a BillboardGui with a MaxDistance of about 5 instead of doing this loop. You could fire a function to get the distances between you and the CPUs when you press “e”. But only do this when the player presses “e”, so that it’s not running at hyperspeed to check for opening the gui.

I want to have a Prompt that says press E when you are near. My dialogue works but I want to avoid the loop. This is what I have now

I check for the position and if they are near a npc then it pops up the prompt.