How to enable gui when a part is touched

So I have a part which when is touched a gui is shown, i tried my script but it didnt work and also didnt give any errors , any reason why?

local ebutton = button.eButton

function you(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		button.Visible = true
		ebutton.Visible = true
	end
end

script.Parent.Touched:Connect(you)
1 Like

Is this a LocalScript? If yes then what is its location?

Edit: Nevermind I see its patent is part, LocalScript don’t work in workspace

You should read this article Roblox Client-Server Model

i tried putting it in startergui which also didnt make it work

Where is button defined?? Is this in StarterCharacterScripts or StarterGui. Either ways its better practice to get that players ui from the PlayerGui.


function hitPart(hit)
   if hit.Parent:FindFirstChild("Humanoid") then
       local player = game.Players:GetPlayerFromCharacter(hit.Parent)
       local UI = player.PlayerGui.Frame.YourUI
       UI.visible = true
   end
end

part.Touched:Connect(hitPart)
2 Likes

You probably need to use RemoteEvent to send signal from Script in part (when the part is touched) to LocalScript in your gui (to enable it)

You can’t access the Player GUI from the Server Script unless it was fired from the Client, In this case, you’re using Touched event, But you can still get the Player.

By following this method;

function you(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
                plrGui = plr.PlayerGui
              --Make the Code here to make the GUI Enable
	end
end

script.Parent.Touched:Connect(you)
2 Likes

touched event will run on the client

local Part = game.Workspace["YOUR PART HERE"]
local Gui = script.Parent -- This means the script is parented already into the gui than getting it into the PlayerGui

local function OnTouched(Object)
	if game.Players:GetPlayerFromCharacter(Object.Parent) then
		Gui.Visible = true
	end
end

Part.Touched:Connect(OnTouched)
2 Likes

StarterGui is where the button is placed in

Should i put the script under the part which is gonna get touched or the ui which is gonna dissapear