Make Gui Enabled At Certain Distance From Part, Disabled At Same Distance

Hello everybody, I want to make a system where a ScreenGui is enabled upon reaching a certain distance to a part, and is disabled upon leaving that same distance. I do not know where to start with this, all I have is the actual Gui in StarterGui called “PickUpItem” and the part that I want to measure the player’s distance called “DistanceDetect” which is a child of a model called “UndeadSoul”.

The gui itself contains 2 frames, 1 with a text button that will eventually be scripted to create a specified item for the player. After this, the ability of the gui to appear will be disabled permanently.

1 Like

You can use DistanceFromCharacter to know if it is within a certain range

local Target = -- Part
local Limit = 20

local Player = game:GetService("Players").LocalPlayer
local GuiObject =  -- Any object you want to hide/show

game:GetService("RunService").Stepped:Connect(function()
	GuiObject.Visible = (Player:DistanceFromCharacter(Target.Position) <= Limit)
end)

To keep it up to date, you can use Stepped.

Thank you for this, and I see how it makes sense. I added the part name and GuiObject name, but got this message in output: Workspace.UndeadSoul.DistanceDetect.Script:9: attempt to index nil with ‘DistanceFromCharacter’ - Server - Script:9

As it is a server script, LocalPlayer is nil, change the script to localscript.

Try providing your script in future & any errors which appear in console.

I have been trying to adjust various things to make this script work, to no avail. Here is the script:

local Target = script.Parent
local Limit = 20

local Player = game:GetService("Players").LocalPlayer
local GuiObject = game.StarterGui.PickUpItem

game:GetService("RunService").Stepped:Connect(function()
	GuiObject.Enabled = (Player:DistanceFromCharacter(Target.Position) <= Limit)
end)

This is a local script inside of the object that I want to measure distance and therefore indicate whether the GuiObject (called “PickUpItem”) is enabled. Please give any suggestions on how to make the Gui visible from a certain distance.