Attempt to index nil with "WaitForChild"

So I have a store, there’s an item that the player has to buy, it’s a dumbbell. The item also has an indicator that it should pump, but for some reason, it is equal to nil. BUT, when I tested in the studio all worked, when I restarted also all works, but in some situations does not work. Why?

Local:

-- ПЕРЕМЕННЫЕ
local player = game:GetService("Players").LocalPlayer
local button = player.PlayerGui:WaitForChild("ShopGui").shopMain.buyThingButton2
local equipButton = player.PlayerGui:WaitForChild("ShopGui").shopMain.equipButton2
local character = player.Character

local tool = game:GetService("ReplicatedStorage"):WaitForChild("Tools"):FindFirstChild("Dumbbell3") or player:WaitForChild("Tools"):FindFirstChild("Dumbbell3")

--if tool.Parent == game:GetService("ReplicatedStorage"):WaitForChild("Tools") then
--	button.Visible = true
--elseif tool.Parent == player:WaitForChild("Tools") then
--	button.Visible = false
--end

local ld = player:WaitForChild("leaderstats")
local main = script.Parent.Parent.ShopGui.shopMain
local tween = game:GetService("TweenService")

local buyTool = game:GetService("ReplicatedStorage"):WaitForChild("Remotes").buyTool

local function close()
	game:GetService("SoundService").click:Play()
	local Info = TweenInfo.new(0.15)
	local Tween = tween:Create(main, Info, {Position = UDim2.new(0.205, 0, 1, 0)})
	Tween:Play()

	game:GetService("Lighting").Blur.Enabled = false
	tween:Create(game.Workspace.CurrentCamera, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70}):Play()
end

local function buy() -- ПОКУПАЕМ ПРЕДМЕТ
	if player:WaitForChild("leaderstats").Wins.Value >= 50 then -- ЕСЛИ ДЕНЕГ ХВАТАЕТ
		tool.Parent = player:WaitForChild("Tools")
		button.Visible = false
		player:WaitForChild("leaderstats").Wins.Value -= 50
		buyTool:FireServer(tool.Name)
	end
end

local function getTool(stat) -- ИЩЕМ ТЕКУЩУЮ ГАНТЕЛЮ
	local backpackItems = player.Backpack:GetChildren()
	local equipped = player.Character:FindFirstChildOfClass("Tool")

	local warehouse = player:WaitForChild("Tools"):GetChildren()

	if equipped and equipped.stat.Value == stat then -- ИЩЕМ ПРЕДМЕТ В РУКЕ
		return equipped
	end

	for _, tool in pairs(backpackItems) do -- ИЩЕМ В ИНВЕНТАРЕ
		if tool.stat.Value == stat then
			return tool
		end
	end

	for _, tool in pairs(warehouse) do -- ИЩЕМ НА СКЛАДЕ
		if tool.stat.Value == stat then
			return tool
		end
	end

end

local function equip() -- ЭКИПИРОВЫВАЕМ
	local statTool = tool.stat.Value
	local currentTool = getTool(statTool)

	currentTool.Parent = player:WaitForChild("Tools")
	game:GetService("ReplicatedStorage"):WaitForChild("Settings").currentTool.Value = tool.Name
	tool.Parent = character
	close()
end

-- ВЫЗОВЫ
button.MouseButton1Click:Connect(buy)
--button.TouchTap:Connect(buy)

equipButton.MouseButton1Click:Connect(equip)
--equipButton.TouchTap:Connect(equip)
3 Likes

What is equal to nil? I don’t know what part you’re talking about.

2 Likes

The current waitforchild cannot find the tools, did you check in backpack or sum?

1 Like

It looks like the tool you are indexing is nil but we don’t know what lines the errors are talking about because we can’t see line numbers.

1 Like

isn’t Tools Folder in ReplicatedStorage, or do you clone it into the player

I mean stat. tool.stat.Value. character limit

Can you possibly send the specific line that breaks?

You have not send the script which the :WaitForChild() error happens in so we can not really help you any further, but generally this happens because the instance you are casting WaitForChild on is nil.

Attempt to index nil with ‘stat’ means that the instance stat does not exist, maybe you forgot to create it, put in the wrong path or it gets create a bit too late and you have to use :WaitForChild(“Stat”).

Do you know that you need a remote event to do the buy function ?
Decreasing Wins value in client side is not decreasing it for real, server will still see the value as it was before.
As well as for the tool equip, because tool client access is easy exploitable.

This is incorrect, this means that the parent of stat is nil therefore it can’t index stat. The tool is nil in this case. Possibly misnamed.

1 Like

Well, if you say it worked on studio, are you actually sure you also published the place? Maybe you had a faulty script, fixed it but forgot to publish, because honestly I would not know a reason it works in studio but not ingame.