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

Incorrect use of GetPlayerFromCharacter() ?

Asked by 9 years ago

I tested my game and instantly saw an error about Argument 1 missing or nil

Here's the script (The problem is Line 1)

local Playar = game.Players:GetPlayerFromCharacter()
local Start = Playar.PlayerGUI
--[[local RobuxGUI = game.ReplicatedStorage["R$GUI"]:Clone()
        RobuxGUI.Parent = Start]]


script.Parent.Touched:connect(function(Hit)
    local Character = Hit.Parent
    if Start["R$GUI"] ~= nil or Start["HelpGUI"] ~= nil then
        print("")
    elseif Start["R$GUI"] == nil or Start["HelpGUI"] == nil then
        local RobuxGUI = game.ReplicatedStorage["R$GUI"]:Clone()
        RobuxGUI.Parent = Start
    end
end)

If I'm using this GetPlayerFromCharacter function wrong, may I be given an example or something to help?

2 answers

Log in to vote
0
Answered by 9 years ago

You need the character model in between the "()" in get player from character.

script.Parent.Touched:connect(function(Hit)
    local Character = Hit.Parent
local Playar = game.Players:GetPlayerFromCharacter(Character) --You need the character in this argument
local Start = Playar.PlayerGUI
--[[local RobuxGUI = game.ReplicatedStorage["R$GUI"]:Clone()
        RobuxGUI.Parent = Start]]
    if Start["R$GUI"] ~= nil or Start["HelpGUI"] ~= nil then
        print("")
    elseif Start["R$GUI"] == nil or Start["HelpGUI"] == nil then
        local RobuxGUI = game.ReplicatedStorage["R$GUI"]:Clone()
        RobuxGUI.Parent = Start
    end
end)

Not really that complicated. Hope it helps!

Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

I would like to add that

 if parent.child ~= nil then

won't work.

Before you check if it doesn't equal nil, you're already assuming there actually is an object to check! While checking if it exists, you're already assuming that "it" does indeed exist. If "it" doesn't exist, you just indexed a nil value, and the script will error.

You need to use the FindFirstChild() method. It will look for the child, and return that child if it exists. If not, it will return nil. Since it works like a function, instead of throwing an error it will simply become nil.

if parent:FindFirstChild("child") ~= nil then
0
Can you please post this on my other question about why my above script doesn't work(Not exactly that)? Then i'll accept answer because it's more relevant alphawolvess 1784 — 9y

Answer this question