Attempt to index nil with 'WaitForChild'

This is my second post today and once again I’m having a pretty frequent problem amongst other people but I find my situation to not be resolved in any other posts

I’m trying to declare the leaderstats folder from the LocalPlayer but I’m getting thrown with an error message, here is my script:

local ts = game:GetService("TweenService")

local players = game:GetService("Players")
local player = players.LocalPlayer

local leaderstats = player:WaitForChild("leaderstats")
local money = leaderstats:FindFirstChild("Money")

local producer = script.Parent.Parent.producer
local producerChildren = producer:GetChildren()

local button = script.Parent


button.Touched:Connect(function()
	local function producerTween()
		for i, v in pairs(script.Parent.Parent.producer:GetDescendants()) do
			local producerInfo = TweenInfo.new(
				0.5, -- time
				Enum.EasingStyle.Linear, -- easing style
				Enum.EasingDirection.InOut, -- easing direction
				0, -- repeat count
				false, -- reverses
				0 -- delay
			)
			
			local producerGoal = {Transparency = 0}
			
			local producerTween = ts:Create(v, producerInfo, producerGoal)
			producerTween:Play()
		end
	end
	
	
	local buttonInfo = TweenInfo.new(
		0.5, -- time
		Enum.EasingStyle.Linear, -- easing style
		Enum.EasingDirection.InOut, -- easing direction
		0, -- repeat count
		false, -- reverses
		0 -- delay
	)
	
	local buttonGoal = {Transparency = 1}
	
	local buttonTween = ts:Create(button, buttonInfo, buttonGoal)
	
	if money <= 100 then
		producerTween()
		buttonTween:Play()
		
		task.wait(.5)

		button.CanTouch = false
	else
		print("poor")
	end
end)

Pretty much everything below the initial declarations are irrelevant (probably) but I threw it in just in case

I’m relatively new to scripting so any feedback and/or support would be much appreciated

Edit: typo smh

Your player argument is nil. The game.Players.LocalPlayer reference only exists within the client environment(aka a LocalScript). If you’re using a server script for this you need to fetch the player through another way.

1 Like

That’s what I figured, thanks for the help!

This isn’t a LocalScript, which puts Players.LocalPlayer nil on server. Fetching the player on the server requires you to listen to the Players.PlayerAdded event.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.