"attempt to index nil"

local Inventory = game.Players.LocalPlayer["Inventory"]

Screenshot 2022-07-23 181920

I’m getting the error that this index doesn’t exist? I’ve tried using WaitForChild.

Note: Inventory only appears under player upon game startup. (its cloned over)

1 Like

Make sure this is a local script.

1 Like

Actually, the script is in ServerScriptService. How can I get around this?

Under converting the script, it just doesn’t work. No errors, though.

Your using Inventory but not backpack?

LocalPlayer just won’t work on a server script.

game.Players.PlayerAdded:Connect(function(Player) -- When a player joins
    local Inventory = Player:WaitForChild("Inventory") -- We wait till inventory loads
end)

try local Players = game.Players

local player is for localscripts.
@UltraConstructor6 could you show us your script?

I guess it’s a custom inventory system to count game stats, Backpack should only be used for tools.

2 Likes

maybe we could specify whats in the inventory?



game.Players.PlayerAdded:Connect(function(Player) -- When a player joins
    local Inventory = Player:WaitForChild("Inventory") 
   local Metal = Player:WaitForChild(“Inventory”).Metal
   local Wood = Player:WaitForChild(“Inventory”).Wood
end)

wait(3)
local Inventory = game.Players.LocalPlayer:WaitForChild("Inventory")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = ReplicatedStorage:WaitForChild("Tools")
local CraftTool = ReplicatedStorage:WaitForChild("CraftTool")
local craftingInfo = require(ReplicatedStorage:WaitForChild("CraftingInfo"))

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Wood = Inventory:FindFirstChild("Wood") 
	Wood.Parent = leaderstats

	local Rock = Inventory:FindFirstChild("Rock")
	Rock.Parent = leaderstats

	local Metal = Inventory:FindFirstChild("Metal")
	Metal.Parent = leaderstats

end)

CraftTool.OnServerInvoke = function(player, toolName)
	local leaderstats = player.leaderstats
	local crafted = false

	for i, v in pairs(craftingInfo.toolName) do
		local material = leaderstats:FindFirstChild(i)
		if material then 
			if material.Value >= v then
				material.Value = material.Value - v
				crafted = true
			else 
				crafted = false
			end
		end
	end
	if crafted == true then
		local tool  = Tools:FindFirstChild(toolName):Clone()

		if tool then
			tool.Parent = player.Backpack
		end
	end
end

LocalPlayer does not exist for server-sided scripts, only LocalScripts. Have you tried using RemoteEvents to communicate between client and server to accomplish whatever task you’re trying?
https://create.roblox.com/docs/scripting/networking/remote-events-and-functions

Also, adding onto this:

Your original post shows that you’re trying to store values instead of tools in the Inventory folder, Backpack should only be used for Tool objects, it doesn’t support anything else. (EDIT: It won’t throw an exception if you put them in backpack, though)

Overall, I’m not sure what you’re attempting to accomplish here. Can you be more direct?

When trying this, this part of the script errored.

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Wood = Inventory:FindFirstChild("Wood") 
	Wood.Parent = leaderstats

	local Rock = Inventory:FindFirstChild("Rock")
	Rock.Parent = leaderstats

	local Metal = Inventory:FindFirstChild("Metal")
	Metal.Parent = leaderstats

I was confused, I’m sorry. You’re right, I am storing intvalues.

I feel like this is unnecessary


local Wood = Inventory:FindFirstChild("Wood") 
	Wood.Parent = leaderstats -- your already looking for wood and your parent is leaderstats already.

	local Rock = Inventory:FindFirstChild("Rock")
	Rock.Parent = leaderstats

	local Metal = Inventory:FindFirstChild("Metal")
	Metal.Parent = leaderstats

1 Like

could you show me your error message in the developer console, or is it just the same?

What covers for it?
text filling requirement

You’re still trying to get LocalPlayer on a server script, LocalPlayer will only exist on the client side. This makes Inventory equal to nil. Please consider using RemoteEvents and/or PlayerAdded:Connect()

Here’s everything!

Tactical Foraging {WIP} @ 23 Jul 2022 19:52 auto-recovery file was created - Studio
19:52:14.138 ServerScriptService.Script:15: attempt to index nil with ‘FindFirstChild’ - Server - Script:15
19:52:14.138 Stack Begin - Studio
19:52:14.138 Script ‘ServerScriptService.Script’, Line 15 - Studio - Script:15
19:52:14.139 Stack End - Studio
19:52:19.724 ServerScriptService.Script:30: invalid argument #1 to ‘pairs’ (table expected, got nil) - Server - Script:30
19:52:19.724 Stack Begin - Studio
19:52:19.724 Script ‘ServerScriptService.Script’, Line 30 - Studio - Script:30
19:52:19.724 Stack End - Studio
19:52:19.741 ServerScriptService.Script:30: invalid argument #1 to ‘pairs’ (table expected, got nil) - Client - LocalScript:9
19:52:19.741 Stack Begin - Studio
19:52:19.741 Script ‘Players.UltraConstructor6.PlayerGui.MainGui.CraftingFrame.LocalScript’, Line 9 - Studio - LocalScript:9
19:52:19.741 Stack End - Studio
19:54:59.960 Disconnect from ::ffff:127.0.0.1|64289 - Studio

How can I utilize these in this particular code? I’m sorry, I’m super new.

Instead of FindFirstChild you should WaitForChild since your basically already trying to find “Inventory” i think.


local Wood = Inventory:WaitForChild("Wood")
	Wood.Parent = leaderstats