Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Hit isn't connecting to the player?

Asked by 7 years ago

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.

0
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 TheeDeathCaster 2368 — 7y
0
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! :) HornedWyven 10 — 7y
0
Np! :D Glad to be of help. :) TheeDeathCaster 2368 — 7y

2 answers

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago

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)
0
lol you don't require "== false," and I don't recommend using line 6, b/c what if there's an object that's the same name as the player in Players? :P TheeDeathCaster 2368 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

  1. FindFirstChild

  2. End - Ends a chunk or function, signifying when the code starts & ends

  3. If statement

  4. 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).

  5. GetPlayerFromCharacter

  6. Booleans (true/ false values)

  7. Not operator

I hope my answer helped you in any way! :)

Answer this question