Close GUI when walking away

Hi,
I’m making a shop GUI, and it opens with a proximity prompt. That works perfectly fine. Now, I’d like it to close when you walk away (10 studs distance). I tried using .Magnitude but that didn’t seem to work, and it didn’t yield any errors. Am I doing it wrong? Any help would be greatly appreciated.

local prox = shopkeeper.ProximityPrompt
prox.Triggered:Connect(function(player)
	player.PlayerGui.ScreenGui.ShopFrame.Visible = true
	player.PlayerGui.ScreenGui.Icons.Visible = false
	player.CharacterAdded:Connect(function(character)
		local HRP = character:WaitForChild("HumanoidRootPart")
		local dist = (shopkeeper.Position - HRP.Position).Magnitude
		if dist > 10 then
			player.PlayerGui.ScreenGui.ShopFrame.Visible = false
			player.PlayerGui.ScreenGui.Icons.Visible = true
		end
	end)
end)

The ProximityPrompt should have an Active property that signifies when it’s visible, and in range. You could just check for that property being false, then close the gui!

1 Like

Instead of .Magnitude you could use player:DistanceFromCharacter(shopkeeper.Position) and test if it is greater than 10

I don’t see an Active property in Properties, and I attempted scripting this, and it also yielded that Active did not exist as a property

local prox = shopkeeper.ProximityPrompt
prox.Triggered:Connect(function(player)
	player.PlayerGui.ScreenGui.ShopFrame.Visible = true
	player.PlayerGui.ScreenGui.Icons.Visible = false
	if prox.Active == false	 then
		player.PlayerGui.ScreenGui.ShopFrame.Visible = false
		player.PlayerGui.ScreenGui.Icons.Visible = true
	end
end)

Nope, sadly this didn’t work either for me. No errors in output either.

local shopkeeper = game.Workspace.Lobby.Shop.Mine.Dwasimwiz1
local prox = shopkeeper.ProximityPrompt
prox.Triggered:Connect(function(player)
	player.PlayerGui.ScreenGui.ShopFrame.Visible = true
	player.PlayerGui.ScreenGui.Icons.Visible = false
	local dist = player:DistanceFromCharacter(shopkeeper.Position)
	if dist > 10 then
		player.PlayerGui.ScreenGui.ShopFrame.Visible = false
		player.PlayerGui.ScreenGui.Icons.Visible = true
	end
end)

Where is the script inside of?

It’s a local script and it is parented to ShopFrame

Sorry, forgot to update the code…

Here, use this:

local shopkeeper = game.Workspace.Lobby.Shop.Mine.Dwasimwiz1
local prox = shopkeeper.ProximityPrompt
prox.Triggered:Connect(function(player)
	if player.Name == game.Players.LocalPlayer.Name then
		script.Parent.Visible = true
		script.Parent.Parent.Icons.Visible = false
		
		while wait(0.5) do
			local dist = player:DistanceFromCharacter(shopkeeper.Position)
			if dist > 10 then
				script.Parent.Visible = false
				script.Parent.Parent.Icons.Visible = true
			end
		end
	end
end)

Sorry about that…

This will update every half second, so it should be okay.

1 Like

PromptHidden() is the script connection that you might be looking for!

Screenshot_20220821-182001_Opera

3 Likes

I tested the code with text buttons in the “Shop Frame” and everything works fine.

As this is run on the client you may as well get rid of the distance check and just rely on the Proximity prompt show / hide distance.That way you won’t be showing unusable prompts to players.

Great, this works! Thank you so much!

Indeed, PromptHidden() would be an alternative as well.