Showing a GUI when a player is touching a part

Beginner programmer here, working on a script that will show a GUI when a player is touching a part and it disappear when a player is no longer touching it. (The part is invisible, essentially it’s used as a hitbox)

Nothing populates in the output when touching the part to give me any insight on what the issue is.

Before, I had it set as a server script rather than local before understanding that StartGui and PlayerGui are entirely different entities, however, observed that it did work for enabling the Gui within StarterGui and disabling it as intended. The LocalScript is placed within the “hitbox” part.
image
Further, this is what it looks like when running an instance of the game.
image

local myPart = script.Parent
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local myGui = playerGui:WaitForChild("HowToPlay2")

myPart.Transparency = 1

-- Function to handle when the part is touched
local function onTouch()
	--Enable the GUI
	myGui.Enabled = true
end

-- Function to handle when the part is no longer touched
local function onUnTouch()
	--Disable the GUI
	myGui.Enabled = false
end

-- Connect the onTouch function to the Touched event of the part
myPart.Touched:Connect(onTouch)

-- Connect the onUnTouch function to the TouchEnded event of the part
myPart.TouchEnded:Connect(onUnTouch)

I’m not entirely knowledgeable with coding/scripting and more of a builder/3D modeler and this is my first game I’ve actually gained some ground on. Working on a very small project (Essentially whack a mole with GUI) to learn some coding processes and I’ve gotten stuck here.

1 Like

It’s a local script. LocalScripts do not run in the workspace.

This is the adjusted code:

local Players = game:GetService("Players")
local myPart = script.Parent

myPart.Transparency = 1

-- Function to handle when the part is touched
local function onTouch(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent) --// Uses Players to get the player from the character, which is the parent of the object that triggered the touched event.

	if player then --// Checking if Players:GetPlayerFromCharacter actually returned something
		player.PlayerGui.HowToPlay2.Enabled = true
	end
end

-- Function to handle when the part is no longer touched
local function onUnTouch(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent) --// Uses Players to get the player from the character, which is the parent of the object that triggered the touched event.

	if player then --// Checking if Players:GetPlayerFromCharacter actually returned something
		player.PlayerGui.HowToPlay2.Enabled = false
	end
end

myPart.Touched:Connect(onTouch)
myPart.TouchEnded:Connect(onUnTouch)

The LocalScript should be changed to a Script.

1 Like

It does.
Though you’re right on the workspace part.

LocalScripts do not run in the workspace. The only places they run are StarterScripts, StarterGui, and inside tools.

adding on to this, they may have to use the :GetPlayerFromCharacter() function.

Nah. No need since this isn’t a server script.
What they can do is they can put that LocalScript into the UI itself, and when the part gets detected, they can disable or enable it.

if they were to replace the script as a server script, then yeah, but if they keep the local script that’s what they would do. Really depends.

Edit: forgot that you can’t make a GUI visible on the server, my bad

Oops, you’re right. I guess I didn’t even realize I typed that before sending my post originally, lol.

1 Like

This worked perfectly when placing the script into the Gui as you said, thank you!

local myGui = script.Parent
local myPart = game.Workspace.Book_of_Learning

myPart.Transparency = 1

-- Function to handle when the part is touched
local function onTouch()
	--Enable the GUI
	myGui.Enabled = true
end

-- Function to handle when the part is no longer touched
local function onUnTouch()
	--Disable the GUI
	myGui.Enabled = false
end

-- Connect the onTouch function to the Touched event of the part
myPart.Touched:Connect(onTouch)

-- Connect the onUnTouch function to the TouchEnded event of the part
myPart.TouchEnded:Connect(onUnTouch)

Alright, though is this specifically for the LocalPlayer? (Like for example, when someone else touches it, it won’t display?)

(If it’s for only the local player (basically yourself), then you can add the
if Players:GetPlayerFromCharacter(onTouch.Parent) ~= Players.LocalPlayer then return end
to both of the touch functions. (make sure to add this statement before myGui.Enabled))

Testing this, yes this is exactly what I am going for. I didn’t want it to show for the whole server. I’ll mess around with this input! Thank you again.

local myGui = script.Parent
local myPart = game.Workspace.Throne_Room.Book_of_Learning
local Players = game.Players

-- Function to handle when the part is touched
local function onTouch()
	--Enable the GUI
	if Players:GetPlayerFromCharacter(onTouch.Parent) ~= Players.LocalPlayer then return
		end
	myGui.Enabled = true
	end

-- Function to handle when the part is no longer touched
local function onUnTouch()
	--Disable the GUI
	if Players:GetPlayerFromCharacter(onUnTouch.Parent) ~= Players.LocalPlayer then return
		end
	myGui.Enabled = false
end

-- Connect the onTouch function to the Touched event of the part
myPart.Touched:Connect(onTouch)


-- Connect the onUnTouch function to the TouchEnded event of the part
myPart.TouchEnded:Connect(onUnTouch)

Not sure where I’m going wrong exactly?
The Output error states Players.MashedNaters.PlayerGui.HowToPlay2.ShowGUIonTouch:8: attempt to index function with ‘Parent’

You forgot to include the hit part that gets returned from the connection in the functions.

local myGui = script.Parent
local myPart = game.Workspace.Throne_Room.Book_of_Learning
local Players = game.Players

-- Function to handle when the part is touched
local function onTouch(hit)
	--Enable the GUI
    if not hit or not hit.Parent then return end
	if Players:GetPlayerFromCharacter(hit.Parent) ~= Players.LocalPlayer then return end
	myGui.Enabled = true
end

-- Function to handle when the part is no longer touched
local function onUnTouch(hit)
	--Disable the GUI
    if not hit or not hit.Parent then return end
	if Players:GetPlayerFromCharacter(hit.Parent) ~= Players.LocalPlayer then return end
	myGui.Enabled = false
end

-- Connect the onTouch function to the Touched event of the part
myPart.Touched:Connect(hit)


-- Connect the onUnTouch function to the TouchEnded event of the part
myPart.TouchEnded:Connect(hit)
1 Like

Thank you for all of your help, the script is finally working as intended!

local myGui = script.Parent
local myPart = game.Workspace.Throne_Room.Book_of_Learning
local Players = game.Players

-- Function to handle when the part is touched
local function onTouch(hit)
	--Enable the GUI
	if not hit or not hit.Parent then return end
	if Players:GetPlayerFromCharacter(hit.Parent) ~= Players.LocalPlayer then return end
	myGui.Enabled = true
end

-- Function to handle when the part is no longer touched
local function onUnTouch(hit)
	--Disable the GUI
	if not hit or not hit.Parent then return end
	if Players:GetPlayerFromCharacter(hit.Parent) ~= Players.LocalPlayer then return end
	myGui.Enabled = false
end

-- Connect the onTouch function to the Touched event of the part
myPart.Touched:Connect(onTouch)


-- Connect the onUnTouch function to the TouchEnded event of the part
myPart.TouchEnded:Connect(onUnTouch)
1 Like

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