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

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

ss.Touched:Connect(function(tuch)
    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
    end
end)

ss.TouchEnded:Connect(function(tuch)
    pGui.Perks.Enabled = false
end)
0
try :WaitForChild() instead? Spiritlotus 151 — 1mo
0
attempt to index nil with 'WaitForChild' throws the same error LaysCo 46 — 1mo
0
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
2
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)
end
0
thats not the issue that part rest of script works LaysCo 46 — 1mo
1
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
0
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
Ad

Answer this question