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 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
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')
if PlayerGui then
if not PlayerGui.Password.Frame.Visible then
PlayerGui.Password.Frame.Visible = true
end
end
end
end
end)
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
FindFirstChild
End - Ends a chunk or function, signifying when the code starts & ends
If statement
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).
GetPlayerFromCharacter
Booleans (true/ false values)
Not operator
I hope my answer helped you in any way! :)
3 comments
The variable of the Touched event/ function (in this vase) returns the child/ object that touched the specific part; it doesn't return the player; I recommend using the GetPlayerFromCharacter function to counter this. :) This article explains how it's used & how it works: http://wiki.roblox.com/index.php?title=API:Class/Players/GetPlayerFromCharacter
Another problem surfaced, but it's a different topic so I'll carry it over to a new question, but this part of it works now. Thanks so much! :)
Np! :D Glad to be of help. :)