Local Player Issues

Hi, I’m trying to make it so that when the player joins, the script will look over their script inventory but i’m getting this error: ServerScriptService.InventoryManager:19: attempt to call a nil value

here is my code:

game.Players.PlayerAdded:Connect(function(player)
	local skinInventoryFolder = player:WaitForChild("SkinInventory")
	local trapInventoryFolder = player:WaitForChild("TrapInventory")
	local skinInventory = skinInventoryFolder:GetChildren()
	local trapInventory = trapInventoryFolder:GetChildren()
	local allSkins = game.ReplicatedStorage:FindFirstChild("AllSkins")
		-- Skin Variables --
	local BlueBeast = allSkins["Blue Beast"]
	local GreenBeast = allSkins["Green Beast"]
	local MegaTheMagnificent = allSkins.MegaTheMagnificent
	local Noob = allSkins.Noob
	local OrangeBeast = allSkins["Orange Beast"]
	local RedBeast = allSkins["Red Beast"]
	local SmokyBeast = allSkins["Smoky Beast"]
	local Summertime = allSkins.Summertime
	local WooleyWool = allSkins.WooleyWool
	
	while true do
		if skinInventory:WaitForChild("Blue Beast") then
			BlueBeast:Clone()
			BlueBeast.Parent = skinInventoryFolder
		end
wait(1)
end)

Has to do with this line. Don’t know what’s wrong

If you’re meaning to guarantee its existence, do

BlueBeast = skinInventory:WaitForChild("Blue Beast", 5)
 -- since you already defined it as a variable above
 -- now whatever you do here will run after 'Blue Beast' existed, because WaitForChild yields until the instance is found, otherwise times out with no other arguments provided after 5 seconds
 -- there's no need to do 'if Blue Beast then' here unless anything called 'Blue Beast' is not bound to exist at all
 -- clone here

What’s the use of the while loop here?

Unless you want to clone the object infinitely, remove the loop entirely.

On another note, Instance:Clone() is not being used appropriately,
when you clone the instance once, the cloned object’s parent will have been set to nil, and the BlueBeast's parent the skinInventoryFolder, so doing this for multiple players will just clone the object once more and swap the original object’s parent each time.

If you meant to clone the instance to the folder instead and leave the original object where it was, set the cloned object’s parent to the folder instead

BlueBeast:Clone().Parent = skinInventoryFolder

Where would this go? in place of the if statement?

I already addressed that,
just replace everything within your entire while loop, hopefully you read my entire response.

In your original code, an end is missing too, for the while loop.

 BlueBeast = someFolder:WaitForChild("Blue Beast")
 local clone = BlueBeast:Clone()
 clone.Parent = skinInventoryFolder

Edit:
I just noticed you’re calling WaitForChild on an array, it’s meant to be used on objects instead, please debug your code before posting.

Define skinInventory as the folder, not its children.
I’m just assuming context here too.

1 Like

Still gettinng this error: ServerScriptService.InventoryManager:19: attempt to call a nil value

Hmm: ServerScriptService.InventoryManager:19: attempt to index nil with ‘Clone’