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

Can't find PlayerGui trying to get the player from the character?

Asked by 7 years ago
Simple, step on a brick and a GUI clones into your PlayerGui from Replicated Storage. 

For some reason it always says that on line 7 the attempt to index a local "human" is nil? How? I'm missing something pretty simple here and I cant figure out what it is. I've tried multiple workarounds all of them have issues finding the PlayerGui.
local t = false

script.Parent.Touched:connect(function(hit)
    if t == false then
        t = true
        wait(1)
        local human = game.Players:FindFirstChild(hit.Parent.Name)
        local jo = game.ReplicatedStorage.JOBS.Cashier:Clone(human.PlayerGui)   
        script.Parent.Transparency = 1
        script.Parent.CanCollide = false
        t = false
        wait(600)
        script.Parent.Transparency = 0
        script.Parent.CanCollide = true
    end
end)

Any help is greatly appreciated this is getting to me.

0
have you tried doing print(hit.Parent.Name) Make sure that is returning the correct player name. dragonkeeper467 453 — 7y

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

The Touched event fires whenever something touches the specified object. Perhaps it wasn't a player that touched the object when the event fired? That would make line 7 nil.

Check if 'human' exists before proceeding with the code.

Also, you need to set the Parent property of the cloned object. Supplying an argument to the Clone function when you're invoking it as a method does nothing.

local db = false
local cashier = game.ReplicatedStorage.JOBS.Cashier

script.Parent.Touched:connect(function(hit)
    if not db then
        db = true
         wait(1)
        local human = game.Players:FindFirstChild(hit.Parent.Name)
        if human then
            local jo = cashier:Clone()
            jo.Parent = human.PlayerGui
            script.Parent.Transparency = 1
            script.Parent.CanCollide = false
            wait(600)
            script.Parent.Transparency = 0
            script.Parent.CanCollide = true
        end
        db = false
    end
end)
Ad
Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
7 years ago
Edited 7 years ago

I doubt it errors on line 7, it probably errors on line 8 when you try to index a "human" that you failed to find. Try using more checks to make sure the toucher is a player. By the way , "Clone" does not take a parameter; you must parent it yourself.

local t = false

script.Parent.Touched:connect(function(hit)
    if t == false then
        t = true
        wait(1)

        local char = hit.Parent
        --very useful!
        local human = game.Players:GetPlayerFromCharacter(char)

        if human then
            local jo = game.ReplicatedStorage.JOBS.Cashier:Clone()
            jo.Parent = human.PlayerGui

            script.Parent.Transparency = 1
            script.Parent.CanCollide = false
            t = false
            wait(600)
            script.Parent.Transparency = 0
            script.Parent.CanCollide = true
        end
    end
end)

Answer this question