Gui does not appear when part it touched and unlocked with a tool [Solved Again Lol]

Q: What do you want to achieve?
A: I want the Gui to appear [on everyone’s screen] when the part is touched with a tool.

Q: What is the issue?
A: When the part is touched with the key in hand, the door opens, but the GUI doesn’t appear, thus only making the door open and the alarm sound play.

Q: What solutions have you tried so far?
A: I have tried rewriting the part that doesn’t work multiple times and I tried to look in the DevHub but didn’t find anything to help me so maybe others will help me?

Here’s the code


local part = script.Parent
local wall = game.Workspace.WallToT
local canOpen = true
local Sound = workspace.TWRN
local Txt = game.StarterGui.EntT.TextLabel

local function lift(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
		if humanoid and canOpen then
			local player = game.Workspace:FindFirstChild(partParent.Name)
			if player then
			if player:FindFirstChild('KeyToT') then
				canOpen = false
				wall.Transparency = 1
				wall.CanCollide = false
				player.KeyToT:Destroy()
				Txt.Visible = true          -- Why no work?
				Sound:Play()         -- Works Fine
				wait(10)         -- Works Fine
				Sound:Stop()         -- Works Fine
				Txt.Visible = false         -- Why no work?
			end
		end
	end
end

part.Touched:Connect(lift)

Here’s the image


[hopefully the image link works]

(I also should mention I am a big noob when it comes to scripting and have only now started taking notes on how to script in studio. so plz don’t bully me lol unless you want to, I’ll prob laugh with you tbh.)

(Fun fact: this is my first dev forum post…)

3 Likes

jeez the format of the script sucks… my bad.

2 Likes

try this:

local part = script.Parent
local wall = game.Workspace.WallToT
local canOpen = true
local Sound = workspace.TWRN


local function lift(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA(“Humanoid”)
if humanoid and canOpen then
local player = game.Workspace:FindFirstChild(partParent.Name)
local Txt = player.PlayerGUI.EntT.TextLabel
if player then
if player:FindFirstChild(‘KeyToT’) then
canOpen = false
wall.Transparency = 1
wall.CanCollide = false
player.KeyToT:Destroy()

Txt.Visible = true – Why no work?
Sound:Play() – Works Fine
wait(10) – Works Fine
Sound:Stop() – Works Fine
Txt.Visible = false – Why no work?
end
end
end
end

part.Touched:Connect(lift)
3 Likes

it adjusted it so its playergui rather then starter gui

3 Likes

Hey! Thanks for your response! I’m about to sleep and I just saw your reply!

I used the script and I’m now left very confused!

I get an error when I walk up to the door with the key, it says that PlayerGui is not a valid member of Model “Workspace.(My Username)”

I saw you put in the script PlayerGUI so I charged it to PlayerGui and it still doesn’t work.

I viewed the server in the studio and well yeah PlayerGui is 100% in my character but for some reason, it says it’s not… very odd stuff.

I’m not too sure how to fix maybe you do? idk.

Here is the script in case you want to see it again


local part = script.Parent
local wall = game.Workspace.WallToT
local canOpen = true
local Sound = workspace.TWRN

local function lift(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA(“Humanoid”)
if humanoid and canOpen then
local player = game.Workspace:FindFirstChild(partParent.Name)
local Txt = player.PlayerGui.EntT.TextLabel
if player then
if player:FindFirstChild(‘KeyToT’) then
canOpen = false
wall.Transparency = 1
wall.CanCollide = false
player.KeyToT:Destroy()
Txt.Visible = true
Sound:Play()
wait(10)
Sound:Stop()
Txt.Visible = false
end
end
end
end

part.Touched:Connect(lift)


If i don’t respond its cause I fell asleep, I’ll respond tomorrow if you do

2 Likes

try replace

to

game.PlayerAdded:Connect(function(player)
local part = script.Parent
local wall = game.Workspace.WallToT
local canOpen = true
local Sound = workspace.TWRN

local function lift(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA(“Humanoid”)
if humanoid and canOpen then
local Txt = player.PlayerGui.EntT.TextLabel
if player then
if player:FindFirstChild(‘KeyToT’) then
canOpen = false
wall.Transparency = 1
wall.CanCollide = false
player.KeyToT:Destroy()
Txt.Visible = true
Sound:Play()
wait(10)
Sound:Stop()
Txt.Visible = false
end
end
end
end

part.Touched:Connect(lift)
end)
2 Likes

The “player” variable is actually your character, which doesn’t have the PlayerGui. In order to access the actual player object, you need to use Players:GetPlayerFromCharacter(). So, change

local player = game.Workspace:FindFirstChild(partParent.Name)

to

local player = game.Players:GetPlayerFromCharacter(game.Workspace:FindFirstChild(partParent.Name))

The GetPlayerFromCharacter() method is a part of the Players service and it takes a character model as a parameter and returns the associated player object. Since the PlayerGui is stored inside the player object rather than the character, you only need the player.

Also, don’t forget to have a ScreenGui under the PlayerGui, because every GUI object needs to be parented under a ScreenGui in order to display on a user’s screen.

3 Likes

This will not work because the script creates a new function for every player that joins the game, and connects them to the same event (part.Touched). This means that when any player touches the part, the function will affect every single player in the game.

2 Likes

oh yea true. I didnt notice that

3 Likes

Thank you for responding! I modified the script to match your line, without (hopefully) destroying it like I usually do.

I used print to see if it all went through and it did, however.

I got an error, it said KeyToT is not a valid member of Player “Players.(my username)” which is on line 19, and since it stopped on that line the rest didn’t play out (which I’m pretty sure you already know)


“WHAT HAPPENS IF YOU REMOVE THE DESTROY LINE???”

If I remove the Destroy part the rest works! (kinda), the UI pops up, but others can’t see it pop up. (I should have mentioned in my post that I want everyone to see the Gui) But I also want the key to be destroyed from the backpack.

And yes I did change the destroy line to player.backpack.KeyToT:Destroy() but it still didn’t work, it said somehow it’s not a member of backpack lol.

So my guess is that it has something to do with line 12 which defines what player is, but I’m not an expert.


Here is the script again in case you want to see what I did.

local part = script.Parent
local wall = game.Workspace.WallToT
local canOpen = true
local Sound = workspace.TWRN

local function lift(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and canOpen then
		local player = game.Players:GetPlayerFromCharacter(game.Workspace:FindFirstChild(partParent.Name))
		local Txt = player.PlayerGui.EntT.TextLabel
		if player then player:FindFirstChild('KeyToT') 
				print("Went Through")
				canOpen = false
				wall.Transparency = 1
				wall.CanCollide = false
				player.KeyToT:Destroy()   -- Line 19 KeyToT is not a valid member of Player "Players.(my username)"
				Txt.Visible = true
				Sound:Play() 
				wait(10) 
				Sound:Stop() 
				Txt.Visible = false 
				
			end
		end
	end
part.Touched:Connect(lift)
			 

1 Like

When a player equips a tool, it moves from their backpack into their character. To solve the issue here, we need both the player and the character. In order to do this, simply define game.Workspace:FindFirstChild(partParent.Name) as a separate variable called “character” and use the character variable for the game.Players:GetPlayerFromCharacter() function like this:

-- replace the current player variable with these two lines
local character = game.Workspace:FindFirstChild(partParent.Name)
local player = game.Players:GetPlayerFromCharacter(character)

After this, replace

player.KeyToT:Destroy()

with

character.KeyToT:Destroy()

As for making everyone see the text, you’ll need to loop through every player in the game and access the text label under their PlayerGui. For this, we need to use a “for” loop (I’m going to assume you know how for loops work; but if you don’t, feel free to ask) and game.Players:GetPlayers(). This function returns a table which has every player currently in the server.

-- delete the "Txt" variable and replace "Txt.Visible = true" with this (make sure to play the sound before "wait(10)")
for _, targetPlayer in game.Players:GetPlayers() do
	local Txt = targetPlayer.PlayerGui.EntT.TextLabel
	if Txt then
		Txt.Visible = true
	end
end

wait(10)

-- replace "Txt.Visible = false" with this
for _, targetPlayer in game.Players:GetPlayers() do
	local Txt = targetPlayer.PlayerGui.EntT.TextLabel
	if Txt then
		Txt.Visible = false
	end
end

This code will loop through every player in the game and display the text for them. This has to be done this way since every player has a separate PlayerGui. (You do not need to play the sound for each individual player since the sound is in the workspace)

1 Like

Tysm! I noted down a lot you said, like I said, I’m a noobie lol.

This answer worked 100% for me! I tested 2 accounts and they both heard the audio and saw the GUI

I also thank the UI Designer for attempting to help out as well!

Thank you both! <3

I wanna give you 200 robux but idk how i would do that lol.

I’m glad I was able to help you out. But don’t worry, there is no need for compensation.

1 Like

Hey again! I know you’re probably like “What now.” and I get it if you don’t wanna spend brain cells on helping me with this door thing.

The script worked for 6 days, I’m not too sure what happened but something did.

It’s a really small bug, and all of a sudden, if I walk up to it, it opens even without the key in your backpack, I didn’t edit the script at all, I already know I’m not gonna figure it out lol.

And well there’s only one man I know who could fix this, and it’s you.

here is the script. (no rush to respond btw, I can wait 10 years (and 10 years only) no cap)

local part = script.Parent
local wall = game.Workspace.WallToT
local canOpen = true
local Sound = workspace.TWRN

local function lift(otherPart)

	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and canOpen then
		local character = game.Workspace:FindFirstChild(partParent.Name)
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then player:FindFirstChild('KeyToT')
			    print("Went Through")
				canOpen = false
				wall.Transparency = 1
			wall.CanCollide = false
			character.KeyToT:Destroy()
			for _, targetPlayer in game.Players:GetPlayers() do
				local Txt = targetPlayer.PlayerGui.EntT.TextLabel
				if Txt then
					Txt.Visible = true
				end
			end
			
							Sound:Play() 
				
			wait(10) 
			
							Sound:Stop() 	
			
			for _, targetPlayer in game.Players:GetPlayers() do
				local Txt = targetPlayer.PlayerGui.EntT.TextLabel
				if Txt then
					Txt.Visible = false
				end
			end
		end
	end
end	
part.Touched:Connect(lift)

Use a RemoteEvent and do:

RemoteEvent:FireAllClients()

This will send a message to every client to show your gui.

1 Like

The reason this problem exist is because currently your script doesn’t check whether the player has a key or not. So, we need to change the “if” statement on line 13 (if player then player:FindFirstChild('KeyToT')) to this:

if player and character:FindFirstChild("KeyToT") then

This way we make sure that a player exist and there is a key in their character (the tool).

1 Like

Omg I’m stupid lmao, it’s so funny to me because I was going to try and fix that part cause I had a gut feeling it was there but I didn’t want to mess with it, I should go with my gut more.

Hopefully, I’ll get better at scripting, wish me good luck.

ty!

Don’t be afraid to mess with your code! Failing over and over again until you get it to work is the best way to learn because you figure out how to and how not to do things. Also, I recommend commenting out code instead of deleting them or just create a copy of the script when making huge changes if you’re really worried about messing up. Good luck!

1 Like