Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I detect when a player is close to a part?

:I am working on a script where if a player gets close enough to a door, a billboard gui comes up, and when they move far enough away it dissapears. The billboard gui lets them press e to open or close the door, but how would I detect when and when not the player is close enough to the door?

2 answers

✓ Accepted answer

You use magnitude to determine the distance between two parts. For this situation I would use a while loop.

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local door = workspace.Door
local closestDistance = 20

while true do
	wait(1)
	local distance = (char.HumanoidRootPart.Position - door.Position).magnitude
	if distance <= closestDistance then
		--do code
	end
end

1 comment

0

Thanks a lot, I changed the player detection a little bit but besides that it worked great :)

By Using Magnitude. Place a magnitude function into the part

local magnitude = (part1.Position - part2.Position).magnitude
if magnitude < 5 then
--Put code in here to move door such as Tween or CFrame..
end

ALSO you can place an invisible block in front of and behind the door and use the Touched command like so.

-- place this in the invisible block

Part= script.Parent

Part.Touched:connect(function()
--Put code in here to move door such as Tween or CFrame..
end) 

This discussion is preserved as it was. New answers, comments, and votes are closed.