Still have questions? Join our Discord server and get real time help.
Log in to vote

attempt to index nil with 'FindFirstChild'?

Asked by
LaysCo 46
1 month ago
Edited 1 month ago

So the script is inside a part and when i press play i always get this error in the output if i remove FindFirstChild i then get the error attempt to index nil with 'PlayerGui' and same error with WaitForChild i cant find a way to work around it

local ss = script.Parent
local player = game:GetService("Players").LocalPlayer
local pGui = player:FindFirstChild("PlayerGui") --error happens here
local debounce = false

    if tuch and debounce == false then
        local pGui = game.Players.LocalPlayer:FindFirstChild("PlayerGui")
        local cost = tuch.Cost.Value
        local name = tuch.Name.Value
        pGui.Perks.ValueText.Text = "PRESS F TO PURCHASE COST ["..tostring(cost).."] POINTS"
        pGui.Perks.Enabled = true

    pGui.Perks.Enabled = false
try :WaitForChild() instead? Spiritlotus 151 — 1mo
attempt to index nil with 'WaitForChild' throws the same error LaysCo 46 — 1mo
Layz it is because you are trying to get it on the server, the rest of the script does not work, when you get an error it doesn't run the rest of the script regardless, just stops where the error is, it says attempt to index nil with FindFirstChild because the player is nil since local player is not on the server yumaking 63 — 1mo

1 answer

Log in to vote
Answered by 1 month ago

You cant get the .LocalPlayer in script it only works in a localscript but you can get the player from the tuch param. like this

if tuch.Parent:FindFirstChild("Humanoid") then
    local player = Players:GetPlayerFromCharacter(tuch.Parent)
thats not the issue that part rest of script works LaysCo 46 — 1mo
Yes, the issue is the fact that you are trying to use LocalPlayer on the server, which isn't going to work as the server isn't associated with any of the clients, so LocalPlayer is nil on the server. xInfinityBear 991 — 1mo
my guy, the error literally says that is the issue, it said `attempt to index nil with 'PlayerGui` means u did nil.PlayerGui , means `game:GetService("Players").LocalPlayer returned nil and that returns nil in a script VerdommeMan 1162 — 1mo

Answer this question