How would I wait until a player is near a model

Hi.
I am in the process of making a cafe GUI, and I so far the player clicks on the item they want, but then I want the gui to wait until they are near the machine. Then the gui will say things like “Making drink…” and so on and will make the drink when they are near the machine.

How would I go about doing this?

From what I know, theres two ways to doing this.

The first is to check the players magnitude and see if they’re close to the machine.

The next way is to have a Collision box around the area you want the player to see the GUI. Then use region3 to check whether the player is inside the box or outside the box.

Hope this helped give you an idea! :slight_smile:

2 Likes

To expand on the magnitude thing, you could check the player’s HumanoidRootPart to the model’s magnitude in something like a while loop and if it is less than a certain value execute some code.

Let me know if you need more detail!

1 Like

The absolute easiest way is to make an anchored, no-collide, fully transparent cube around the area in question. Then attach a Touch event to it and check if the part that touches it belongs to a player character model.

1 Like
local Run = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Model = workspace.Model
local ModelRoot = Model.PrimaryPart

Run.Stepped:Connect(function(Delta)
	local Distance = Player:GetDistanceFromCharacter(ModelRoot.Position)
	if Distance <= 10 then --Studs.
		print(Player.Name.." is within 10 studs of the model.")
		--Do other actions here.
	end
end)

I’m surprised no one has suggested “Player:GetDistanceFromCharacter” yet, it essentially handles all of the distance/magnitude calculations for you and returns a number value which represents the distance/magnitude in studs the player’s character is from a specified point in the world space.

https://developer.roblox.com/en-us/api-reference/function/Player/DistanceFromCharacter

5 Likes

Yeah, I was going to answer magnitude too, I think it’s because that’s what most devs are used to and it only works for characters. This looks a lot simpler for that though. I might try using this next time I need distance.

If the character does not exist, :DistanceFromCharacter(Vector3) will return zero.

Correct, although that is stated on the link I provided.