Gui not showing when player touches part

Hi!
I am trying to have it so when a player touches a part a gui shows up, but it’s not working.
I’ve tried looking online and I can’t find anything to help. sorry if I’m missing something obvious, im like rlly new to this. tysm in advance

local touchpart = game.Workspace:WaitForChild("school for unidenified faries")
local bg = script.Parent
local exit = screenGui:WaitForChild("exit_button")
local farm = screenGui:WaitForChild("farmingmode")
local mode = screenGui:WaitForChild("modeselection_text")
local rp = screenGui:WaitForChild("rpmode")
local story = screenGui:WaitForChild("storymode")

touchpart.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild("Humaniod") and touch.Parent == game.Players.LocalPlayer.Character then
		bg.Visible = true
		exit.Visible = true
		farm.Visible = true
		mode.Visible = true
		rp.Visible = true
		story.Visible = true
		print("Gui opened")
	end
end)

(this may help)

4 Likes

No I wouldn’t approach this in that way.
I would make a localscript in “StarterPlayerScripts” that is attached to the player.
SO you can call things like this:

local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Gui = PlayerGui:WaitForChild("<putyourguinamehere>")
local Frame = Gui.Frame

-attach it to a touched event
local function SetupModels()
	for _, descendant in Workspace:GetDescendants() do
		if descendant.Name == "school for unidenified faries" then
			descendant.PrimaryPart.Touched:Connect(function(otherPart)
				local player = Players:GetPlayerFromCharacter(otherPart.Parent)
				if player == Player and not Gui.Enabled then
					Gui.Enabled = true
				end
			end)
		end
	end
end

Hope this helps, but know you have to call certain things that I did not add to most local scripts. My goal is to give you a part of what you need to get you in the RIGHT direction. Then you can use this information above to find a solution. For example this script needs local Workspace = game:GetService(“Workspace”)

Also my advice for you in the future: don’t put scripts into a part, or a NPC or anything like that. Everything can be handled server side. It isn’t good practice and can lead to unwanted activity.

2 Likes

ok, this is the code I think should work but it doesn’t and I keep on getting this error:

Infinite yield possible on ‘Players.iiKhico.PlayerGui.ScreenGui:WaitForChild(“Frame”)’

local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Gui = PlayerGui:WaitForChild("bg")
local exit = PlayerGui:WaitForChild("exit_button")
local farm = PlayerGui:WaitForChild("farmingmode")
local mode = PlayerGui:WaitForChild("modeselection_text")
local rp = PlayerGui:WaitForChild("rpmode")
local story = PlayerGui:WaitForChild("storymode")
local Frame = Gui.Frame

local Workspace = game:GetService(“Workspace”)
--attach it to a touched event

	local function onTouch(hit)
		for _, descendant in Workspace:GetDescendants() do
			if descendant.Name == "school for unidenified faries" then
				descendant.PrimaryPart.Touched:Connect(function(otherPart)
					local player = Players:GetPlayerFromCharacter(otherPart.Parent)
					if player == Player and not Gui.Enabled then
						Gui.Enabled = true
						exit.Visible = true
						farm.Visible = true
						mode.Visible = true
						rp.Visible = true
						story.Visible = true
					end
				end)
			end
		end
	end
2 Likes

That’s from this line: local Frame = Gui.Frame It basically means Frame has not been found or will not be found if it doesn’t exist, can you screenshot the explorer of what your trying to access on that line?

2 Likes

(the highlighted one next to my mouse)

2 Likes

Hmmm… Didn’t you already define “Bg”, in this line local Gui = PlayerGui:WaitForChild("bg") EDIT: think I am looking at the wrong line

2 Likes

yes oopies :sweat_smile: I just got rid of local Frame = Gui.Frame

but now I get this

2 Likes

Ah check your original script, I got confused on the new one, but you misspelled humanoid

local touchpart = game.Workspace:WaitForChild("school for unidenified faries")
local bg = script.Parent
local exit = screenGui:WaitForChild("exit_button")
local farm = screenGui:WaitForChild("farmingmode")
local mode = screenGui:WaitForChild("modeselection_text")
local rp = screenGui:WaitForChild("rpmode")
local story = screenGui:WaitForChild("storymode")

touchpart.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild("Humanoid") and touch.Parent == game.Players.LocalPlayer.Character then
		bg.Visible = true
		exit.Visible = true
		farm.Visible = true
		mode.Visible = true
		rp.Visible = true
		story.Visible = true
		print("Gui opened")
	end
end)

Copy and paste this script back to how you had it when you made this post but with this change I made, it should work

1 Like

it’s still not working :sob:

I got this idk if it will help tho

Players.iiKhico.PlayerScripts.LocalScript:3: attempt to index nil with ‘WaitForChild’

2 Likes

Where are you defining screengui in your code?, line 3 should be: local exit = screenGui:WaitForChild("exit_button")

2 Likes

wdym defining ScreenGUI I thought it was like a built in thing

2 Likes

That is indeed my fault because I told you to copy & paste the script, and I didn’t include the first 2 lines:

local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui 
1 Like

its still not working im trying to fix some of the stuff in the output thing rn

Alright, once your done with that if you’d like to show me what the entire full code that’s erroring from this topic is currently at so I can just get a solid script from the current standpoint to look through, I’ll look through it again, and see what I can do :+1:

ok here it is

local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui 

local touchpart = game.Workspace:WaitForChild("school for unidenified faries")
local bg = script.Parent
local exit = screenGui.modeselectionforunidenifiedfairies("exit_button")
local farm = screenGui.modeselectionforunidenifiedfairies("farmingmode")
local mode = screenGui.modeselectionforunidenifiedfairies("modeselection_text")
local rp = screenGui.modeselectionforunidenifiedfairies("rpmode")
local story = screenGui.modeselectionforunidenifiedfairies("storymode")

touchpart.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild("Humanoid") and touch.Parent == game.Players.LocalPlayer.Character then
		bg.Visible = true
		exit.Visible = true
		farm.Visible = true
		mode.Visible = true
		rp.Visible = true
		story.Visible = true
		print("Gui opened")
	end
end)

this is a screen shot of the error msgs from this script:

and this is a screenshot of the explorer thing if u need it:

1 Like

Okay, so you need to get Players, local Player = game.Players.LocalPlayer Put that in the first line as a replacement, and letme know what happens.

1 Like

it still doesn’t work and I’m getting this msg now

1 Like

Replace the 2nd line in the code with this, local PlayerGui = Player:WaitForChild("PlayerGui", 15)

1 Like

there is still something wrong with the second line

local Player = game.Players.LocalPlayer This is the first line right? I edited it right after I sent it because originally I was being dumb, and made a dumb mistake