How to destroy gui if player gets at a certain distance

I want to destroy a gui if a player gets to a certain distance from an object, how would I go about doing this.

3 Likes

You can use Magnitude to check the distance between two objects. Get the Player’s HumanoidRootPart and the object’s position then check their mag. Then you can mess around with certain distances, let’s say 50. Then set up a loop or use runservice to check if the Magnitude between the two is >= 50(Or the certain distance you want).

1 Like

But how would I get the hrp if the script im planning on using is located in the npc’s uppertorso?

1 Like

hmmm. I would personally go about this one of two ways. I would just have a local script within the Player detecting the Player’s position and the distance between the target object. Then just create and destroy the GUI depending on the distance. If the Distance is <= 50 then I will create the UI, if it is > 50 I would check if the Player has the UI and if they do remove it.

Another way would be creating a giant part that represents the area you want to create/destroy the UI in around the object. If a Player enters this area(Using .touched) then check if they do/don’t have the UI and create/destroy it based on that. This would allow the script to be within the NPC’s uppertorso.

I think the last one may be a little better, but both would work if done correctly.

1 Like

You can use Magnitude so just from your script, something like this:

local Player = game.Players.LocalPlayer
local ScreenGui = Player.PlayerGui.Tattoo2
local TargetObject = workspace.NPCs.WisesNPC.UpperTorso

game:GetService("RunService").Stepped:Connect(function()
	if Player.Character:WaitForChild("Humanoid").MoveDirection.Magnitude > 0 and (TargetObject.Position - Player.Character.PrimaryPart.Position).magnitude < 10 and ScreenGui then
		ScreenGui:Destroy()
		ScreenGui = nil
	end
end)
1 Like

This does not work? Any help as to why?

1 Like

Try Changing PrimaryPart to HumanoidRootPart (although I think it doesn’t really make a difference since it’s usually that) or adjust the < 10 magnitude number to however you want it. If there’s an error than you might want to check what you’re assigning the TargetObject and ScreenGui.

1 Like

I should have probably explained myself, so this gui is given from the npc, so the this script would have to wait for it, any help?

local Player = game.Players.LocalPlayer

local ScreenGui = Player.PlayerGui:WaitForChild("Tattoo2")

local TargetObject = workspace.NPCs.WisesNPC.UpperTorso

boi = false

game:GetService("RunService").Stepped:Connect(function()

if Player.Character:WaitForChild("Humanoid").MoveDirection.Magnitude > 0 and (TargetObject.Position - Player.Character.HumanoidRootPart.Position).magnitude < 10 and ScreenGui == nil and boi == false then

boi = true

ScreenGui:Destroy()

boi = false

end

end)
1 Like

It needs to be a LocalScript and in the PlayerGui, as you can’t exactly run it in a regular script.

1 Like

I did this and it still isn’t working? No output errors.

1 Like

Try debugging such as adding print statements for getting the distance between your character and the part. I’ve tested the code in studio and it should work just fine.

3 Likes

Oh goodness haha, I don’t know where this mixup happend, but my goal was so that when you got farther from the part, it disappeared, not when you got closer. 1 character change and it all worked, Thank you so much though for helping me through this problem of mine.

1 Like

1 last thing, how would I go about letting this “restart” once it happens once it does not work again, how would I go about this?

local Player = game.Players.LocalPlayer

local ScreenGui = Player.PlayerGui:WaitForChild("Tattoo2")

local TargetObject = workspace.NPCs.WisesNPC.UpperTorso

game:GetService("RunService").Stepped:Connect(function()

if Player.Character:WaitForChild("Humanoid").MoveDirection.Magnitude > 0 and (TargetObject.Position - Player.Character.HumanoidRootPart.Position).magnitude > 10 and ScreenGui then

ScreenGui:Destroy()

ScreenGui = nil

game.Workspace.NPCs.WisesNPC.UpperTorso.AlreadyOpened.Value = false

end

end)
1 Like

Nevermind, I just put this up top so it gets the new gui.

game:GetService("RunService").Stepped:Connect(function()

local Player = game.Players.LocalPlayer

local ScreenGui = Player.PlayerGui:WaitForChild("Tattoo2")

local TargetObject = workspace.NPCs.WisesNPC.UpperTorso

if Player.Character:WaitForChild("Humanoid").MoveDirection.Magnitude > 0 and (TargetObject.Position - Player.Character.HumanoidRootPart.Position).magnitude > 10 and ScreenGui then

ScreenGui:Destroy()

ScreenGui = nil

game.Workspace.NPCs.WisesNPC.UpperTorso.AlreadyOpened.Value = false

end

end)
1 Like

Any way to stop the game from checking for “Tattoo2” as its flooding my output with infinite yield possible??

game:GetService("RunService").Stepped:Connect(function()

local Player = game.Players.LocalPlayer

local ScreenGui = Player.PlayerGui:WaitForChild("Tattoo2")

local TargetObject = workspace.NPCs.WisesNPC.UpperTorso

if Player.Character:WaitForChild("Humanoid").MoveDirection.Magnitude > 0 and (TargetObject.Position - Player.Character.HumanoidRootPart.Position).magnitude > 10 and ScreenGui then

ScreenGui:Destroy()

ScreenGui = nil

game.Workspace.NPCs.WisesNPC.UpperTorso.AlreadyOpened.Value = false

end

end)
1 Like

That usually means that the script is running again but can’t find the ScreenGui. You could include a timeout parameter for WaitForChild or use FindFirstChild instead and have the if statement check if the ScreenGui exists.

You seem to have also moved the variables inside the stepped event, which is not really necessary unless you don’t plan to have the script reset via character every now and then.

1 Like