Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

attempt to index nil with 'FindFirstChild'?

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)

3 comments

0

try :WaitForChild() instead?

0

attempt to index nil with 'WaitForChild' throws the same error

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

1 answer

✓ Accepted answer

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

3 comments

0

thats not the issue that part rest of script works

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.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.