Close enough magnitude from LocalPlayer to an object to FireClient an GUI to LocalPlayer

Hello everyone, I hope everyone of you are having an amazing day today. Today I just randomly came up with an idea, that I want to achieve an FireClient (RemoteEvent) GUI when magnitude is close enough to selected object. I will give you an example of what I want to achieve:

  • For example: In Jailbreak, GUI shows up when player is close enough to a car.

My problem is that I don’t really know what would be the best way to do that so I won’t take much server performance or players performance while they are in game.

  • For example: random lags, high ping^^

My idea

What It’s in my mind to try to do this is to do an loop with ‘while true do’ on magnitude value and once that magnitude is below selected magnitude it fires client (RemoteEvent) and paste GUI in PlayerGui. After magnitude is not close enough again it destroys it from PlayerGui.

Please read ‘My idea’ spoiler if you’d like to reply. If there is a simpler way to do this let me know. What I have in my mind is that I try to make this thing to work with lowest taken performance possible. Thank you for your time. :herb:

You can just use BillboardGUIs to achieve this without any server communication? You can call ContextActionService.BindAction with the key you want players to interact with objects with and in the binded function you can call Player.DistanceFromCharacter to get the distance from the object to the character and compare it to the maximum distance of BilboardGui.MaxDistance.

Here’s an example from one of my interaction scripts:

local function Interact(action, state, input)
	if input.KeyCode == Key and state == Enum.UserInputState.End then	
		for i,v in pairs(Interactables) do
			if Player:DistanceFromCharacter(v.Position) < BillboardGui.MaxDistance then
					-- interact with object here
				end
			end
		end
		
	end
end

ContextActionService:BindAction("Interaction", Interact, true, Key)

Hope this helps! :slight_smile:

3 Likes

Please look up your topic before posting there are many tutorials.

Best one:

1 Like

About DistanceFromCharacter: make sure you handle a case of a 0 value as well. DistanceFromCharacter doesn’t handle undefined distances and instead sets it as 0. Missing pieces of a character or an inability for DistanceFromCharacter to calculate will result in being able to activate from anywhere.

Yes I know it’s client-side so it probably doesn’t matter because exploits or whatever but we’re pretending that’s irrelevant and a non-concern. What we’re focusing on is handling edge cases and UX.

2 Likes

I didn’t know that, thanks for the heads up.

1 Like

@metryy Thank you for that, I didn’t know that you can actually call BindAction truth ContextActionService and also for binded function DistanceFromCharacter. This really helped.

@Strongjohnfgamer I know there is more tutorials releated to this topic but this isn’t really the same thing as many others was asking for. I asked for best and most low preformance way to actually do this. Many tutorials now a days are done just to make thing done and are not careful about performance.

@colbert2677 Thank you for teaching us this as well.