This script is inside of a part in workspace called "PassDoor" and the following code is inside a normal script
script.Parent.Touched:connect(function(hit) if hit.Player.PlayerGui.Password.Frame.Visible == false then hit.Player.PlayerGui.Password.Frame.Visible = true end end)
But it isn't working, I don't know how to fix it. Any help is appreciated.
Basically, I'm trying to get it to open a GUI that's inside the player, but I guess the problem is it isn't connecting to the player as I hoped.
Since i see that TheAlphaStigma helped you out, im just going to post the code here may you not figure it out yourself ;D
local timeout = false script.Parent.Touched:connect(function(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) then if timeout == false then timeout = true local p = game.Players[hit.Parent] p.PlayerGui.Password.Frame.Visible = not p.PlayerGui.Password.Frame.Visible wait(1) timeout = false end end end)
B4 I begin, I'd like to give credit to RubenKan for his answer, ideas that I didn't think of at the time, showed what I meant behind my answer-comment-thing, and crediting me. :) Props to ya man! :D
Now, lets begin. :)
First, let's tab your code; makes it look cleaner, and it makes it easier to read:
script.Parent.Touched:connect(function(hit) if hit.Player.PlayerGui.Password.Frame.Visible == false then hit.Player.PlayerGui.Password.Frame.Visible = true end end)
Now it looks much cleaner! :D
Second, let's remove the "== false," and instead use "not," b/c the not operator will return true if a value is "false:"
script.Parent.Touched:connect(function(hit) if not hit.Player.PlayerGui.Password.Frame.Visible then hit.Player.PlayerGui.Password.Frame.Visible = true end end)
Much better. :)
Third, let's edit the code so that it'll check & return the player; we'll use the variable Player to accomplish this:
script.Parent.Touched:connect(function(hit) local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if not hit.Player.PlayerGui.Password.Frame.Visible then hit.Player.PlayerGui.Password.Frame.Visible = true end end)
There we go, we got our variable! :D But, we need to check if the GetPlayerFromCharacter function returned the player, or false! :O Dun dun duuuun!
To accomplish this, we can use the if statement to check & see if the GetPlayerFromCharacter function returned the player - returned true - or returned false! :O More dun dun duuuuuuun!!
script.Parent.Touched:connect(function(hit) local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if Player then -- If the value is true (to the argument), it'll fire the following chunk (code), if not, then it'll return false & will not fire the chunk if not hit.Player.PlayerGui.Password.Frame.Visible then hit.Player.PlayerGui.Password.Frame.Visible = true end end end)
There, now it'll check to see if it was a player that touched the part! :D But, what if the part that touched the part was set to change it's parent to nil after touching a part! :O (Man, that's a tongue-twister e.e).
We can counter this by using the if statement again! Usually, parts that change their parent to nil are known as "bullets," but the name is redundant, as we don't require it to check if the part changed its parent to nil:
script.Parent.Touched:connect(function(hit) if hit.Parent then -- This will check to see if the part that touched the part changed its parent to nil at the time of collision, if its parent didn't change to nil, it'll fire the chunk, if is was... YOU SHALL NOT PASS! local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if Player then if not hit.Player.PlayerGui.Password.Frame.Visible then hit.Player.PlayerGui.Password.Frame.Visible = true end end end end)
Haha! Our counter-strike is in effect! >:D But, there is still work to be done. (Let me guess, D:<)
Now, we need to check to see if "PlayerGui" is existent when the code gets to that part, otherwise the code may error; sometimes people change the name of it, for some some-odd reason(s) e.e; we can counter this by, again, using the if statement! (It's magic!) But it's not alone, we're going to also use variables, & the FindFirstChild function! :O (Murasmus has come!)
script.Parent.Touched:connect(function(hit) if hit.Parent then local Player = game.Players:GetPlayerFromCharacter(hit.Parent) if Player then PlayerGui = Player:FindFirstChild('PlayerGui') -- The FindFirstChild function will loop through the selected parent, and if a child has the name same of the argument, then it'll return the child, if not, it'll return nil if PlayerGui then -- Will check to see if PlayerGui is a child, or nil! >:O if not PlayerGui.Password.Frame.Visible then -- You don't need the 'hit.Parent' thingy-things anymore, since these'll only break your script :P And it'll check if Visible is false at the same time! :D PlayerGui.Password.Frame.Visible = true -- Same change here end end end end -- Forgot to say b4, but the "end" function ends a chunk/ function end) -- Ends a function/ event chunk, thus why there's a ")" at the end of the "end" (See what I did there? ;P)
Now, I'm not going any further, b/c I believe this is enough to get you started, and towards the right path. :)
Stuff touched on but didn't go into great detail about
End - Ends a chunk or function, signifying when the code starts & ends
Nil - Also known as Null in some other language, returns a non-existent value (To dumb-down it a bit, since it'll get confusing :P).
I hope my answer helped you in any way! :)