✓ Accepted answer
You can use the .Magnitude property of a Vector subtraction outcome to calculate the given distance between two provided Positions. By combing this with RunService's .RenderStepped signal, we can perpetually verify the given distance has exceeded before the next frame is rendered
Upon this condition proving true, we can access the PlayerGui container which is used to handle the User Interface, and locate the GUI we wish to remove.
Note:
I suggest you set the .Parent property of an Instance to nil if you wish to remove it but use it again—as expected from getting closer to the Vending Machine, it should appear again. Calling :Destroy() on the Instance will eradicate the Instance from PlayerGui until replaced; you'll have to repeated add the Instance back if so, it will become tedious.
However, since this is a ScreenGui Instance I presume, we can simply just set Enabled to false.
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local part1 = workspace.Part1
local part2 = workspace.Part2
local partDistance = (Part1.Position - Part2.Position).magnitude
local maxDistance = 10
runService.RenderStepped:Connect(function()
if (partDistance > maxDistance) then
local playerGui = player:FindFirstChild("PlayerGui")
if (playerGui and playerGui.VendingGui) then
playerGui.VendingGui.Enabled = false
end
end
end
If this helped, please don't forget to accept this answer!
1 comment
You could do if they walk on a part then it would destroy it (you would put the part where you want). That would make it, so you can remove it easily. Btw, you would probably be using a local script in the part.