Trying to code a when touched part open gui, its not working


image

1 Like

you cannot open gui using StarterGui but instead use PlayerGui
Thats how you can implement that

local Touched_Part = workspace.Part1

local function OnTouch(hit)
if hit.Parent:FindFirstChild("Humanoid") then --checking if its a Players Character
local Character = hit.Parent
local Player = game.Players:GetPlayerFromCharacter("Character") -- Getting Player From Players Character
Player.PlayerGui.ScreenGui.Frame.Visible = true

Touched_Part.Touched:Connect(OnTouch)
1 Like

Hello, thanks for responding are you talking about player gui? Ive just started coding, thats my first ever script

1 Like

Hey LuckyPlanetsAlt1!

StarterGui is actually a conteiner, in easy ways to understand, all of it’s content does not appear on screen and are only stored for later use.

PlayerGui is the GUI that is actually shown on the Player’s screen. When a Player’s Character is loaded, all descendants from StarterGui are copied into PlayerGui.

1 Like

Hello, thanks for responding what do you mean by player gui? Ive just started coding, thats my first ever script

Like where is it located to in the explorer?

1 Like

Hello again LuckyPlanetsAlt1!

PlayerGui is located here:
imagem_2023-09-10_163131462

Ok thanks! Is the script working or is it wrong?

1 Like

image
Its not there

It’s not a creatable Instance; it’s created in the Player’s Instance at runtime

1 Like

What, and when i load into the game attach my script to it?

As the player joins the game stuff in the container gets put into a folder that is located in the player instance.

The player sees the duplicate
So if you change the gui in startergui nothing happens.

This means when the player touches our part we need to get to the player instance and THEN go into the playergui folder located in the player find the gui and then change it

1 Like

So how would we do that, sorry once again this is my first day coding, its most likely terrible what ive done

local playerService = game:GetService(“Players”) — Variable for the players service where we have functions to find players ETC.
local part = script.Parent — Making a variable for our part.

local function touched(intersection)
      local player = playerService:GetPlayerFromCharacter(intersection.Parent)
      if player then — If player exists so that there are no errors.
            player:FindFirstChild(“PlayerGui”):FindFirstChild(“GUIName”).Enabled = true —FindFirstChild is essentially .GUIName but it can contain numbers an spaces.
      end
end

part.Touched:connect(touched)

Note: i wrote this on mobile so there might be an error. Please tell me if there is!

1 Like

@LuckyPlanetsAlt1 Try this!

local part = game.Workspace:WaitForChild("Part") -- Variable for the part.  WaitForChild() waits for the isnatnce to be loaded into the game before the code runs.

local function onTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then -- Checks for the player's Humanoid
		local Character = hit.Parent --  a variable for the character
		local Player = game.Players:GetPlayerFromCharacter(Character) -- Player variable
		Player.PlayerGui.YourGui.Frame.Visible = true -- Shows the GUi.  Set "YourGui" to whatever the name of your GUI is.
	end
end

part.Touched:Connect(onTouched) --Connects the onTouched connect to fire whenever the part is touched.

Hope this helps! It worked for me!

1 Like