The GUI appears only when touching a part and disappears when not in contact with the part

I want the Gui to only appear when the player is touching it.

And i have no idea how to do it.
I have seen some people use magnitude, and some use events. And i wanna go the event route. And i have found no post or video about it

Part.Touched:Connect(function(hit)
  -- check if hit is a character
  -- if true then set gui enabled
end)

Part.TouchEnded:Connect(function(hit)
  -- check if hit is a character
  -- if true then set gui disabled
end)

Normally, I think you are supposed to provide us with some code, and we are supposed to help you fix the problem. However, I understand that you are probably new to the field and need a bit of guidance to get started.

This is the documentation, Here, you can find everything you need to achieve your goal. In this documentation, you can perform a quick search to discover what’s possible to do with a Part. Under Inherited from BasePart, you’ll find a signal named Touched and another signal named TouchEnded. With this information, you now have the basics to accomplish your goal.

However, if you do a quick test

local p = workspace.Part

p.Touched:Connect(function(hit)
	print(hit)
end)

p.TouchEnded:Connect(function(hit)
	print(hit)
end)

We can observe so much print, like that:
RightLowerLeg - Server - Script:4
RightUpperLeg - Server - Script:4
LeftUpperLeg - Server - Script:4
RightFoot - Server - Script:4
Handle - Server - Script:4
RightUpperLeg - Server - Script:8
Handle - Server - Script:4
LeftUpperLeg - Server - Script:8
LeftLowerLeg - Server - Script:4
LeftFoot - Server - Script:4

What’s the point? My point is as follows: it’s a little bit more challenging to determine when the player has completely stopped touching the part. The best way is to create a table to insert all body parts that are in collision with the part.

local p = workspace.Part

local touchRegister = {}

local function GetPlayerByHit(hit)
	-- Find the first ancestor that's a model
	local model = hit:FindFirstAncestorWhichIsA("Model")
	-- Try to find the player from the model
	local player = game:GetService("Players"):GetPlayerFromCharacter(model)
	-- Return the result
	return player
end

p.Touched:Connect(function(hit)
	-- Find if a player touched the part
	local player = GetPlayerByHit(hit)
	-- Verify if player is't nil
	if player then
		-- Create new register for this player
		if (not touchRegister[player]) then
			touchRegister[player] = {}
			print("Start to touch")
		end
		-- Insert part into the player register
		table.insert(touchRegister[player], hit)
	end
end)

p.TouchEnded:Connect(function(hit)
	-- Find if a player touched the part
	local player = GetPlayerByHit(hit)
	-- Verify if player is't nil
	if player then
		-- Find the index of this part in the register
		local index = table.find(touchRegister[player], hit)
		-- Remove from register
		table.remove(touchRegister[player], index)
		-- Remove the register if the player have stopped to touch the part
		if (#touchRegister[player] == 0) then
			touchRegister[player] = nil
			print("Stopped to touch")
		end
	end
end)

It’s not the best way to make appear a UI, using manitude is better. But it’s supposed to working good. Now you can do that

player.PlayerGui.ScreenGui.Visible = true

To display your ScreenGui, or assign ‘false’ to make it disappear, place the code where I’ve added the prints, and you should be good. I apologize if the end of my answer is lacking in explanation; I’m a bit rushed. If my response meets your satisfaction, you can mark it as the solution.

If you have any questions, please don’t hesitate to ask.

Wow. This is a lot. But your explanation is actually rlly good. Thank you very much for helping me out. Although i myself dont rlly understand the script.

Ty

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.