Folder Search function returns nil 90% of the time

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I need my script to return the item that shares the same name as the string passed through the function

  1. What is the issue? Include screenshots / videos if possible!

For some reason this script only works like 10% of the time when I hit the play button, and I have 0 idea why.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried adding wait tasks to see if some things were being added late / in the wrong order, but I cant replicate a state where its working 100% or 0% of the time

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local BuyFunc = game.ReplicatedStorage:WaitForChild("BuyFunc")
local UpdateUI = game.ReplicatedStorage:WaitForChild("UpdateUI")
local brokieSound = script.Fail
local MoneSound = script.Sucsess
local itemsFolder = script.Items
------------------------------------ Define some basline variables ^^^^^^^^^
local function GiveItem(NameOfItem)
--scan throught the items folder looking for the item that shares a name with "Name of item"
	for i,v in pairs(itemsFolder:GetChildren()) do
		if v.Name == NameOfItem then
			--if we find it, clone it into the player's backpack
			return v
		end
	end
end
----------------------^^^^^^ Sorts through a folder looking for an item that is equal to the input thats passed through (Which is "Name of item"), but for SOME REASON it skips the first item in the folder, resulting in nothing being returned

BuyFunc.OnServerEvent:Connect(function(player,ActiveShopTab,ActicveCurrentButton,ActivePrice,ActiveName,SBuyType)
	
	local leaderstats = player:FindFirstChild('leaderstats') or Instance.new("Folder", player)
	local Credits = leaderstats:WaitForChild("Credits ¢")
	local RollLim = leaderstats:WaitForChild("Roll Limit")
	
	--^^^^^^^^^^^^^^ Defines leaderstats, Things like cash and the roll limit of a player
	
	print("=+=+=+= BUY TYPE -- "..SBuyType.Value.." -- BUY TYPE =+=+=+=")

	-- Debug printing ^^

	if Credits.Value <= ActivePrice then
		print("Ha broke")
		brokieSound:Play()
		return
	end
	
	--Checks the money ^^
	
	if	ActiveName == "Roll Limit Upgrade" then
		
		RollLim.Value = RollLim.Value * 1.01
		
	end
	
	--^^Used for the roll limit upgrade
	
	if SBuyType.Value == "Item" then
		
		print("Looking for "..ActiveName)
		
		print(GiveItem(ActiveName))
		--^Returns Nil / nothing at all like 50% of the time^
		
		
		local itemClone = GiveItem(ActiveName):Clone()
		--^Crashes Script^
		itemClone.Parent = player.Backpack
		
	end
	
	Credits.Value = Credits.Value - ActivePrice
	MoneSound:Play()
end)

Is it possible items are being added from the client and read from the server or vice versa?

The reason it may return nil is probably because:

  • ActiveName may be nil and the client is not sending the proper data.

  • The specific item is not present in the folder and so it returns nil

  • The Item IS present in the folder. Just put a simple type check in your GiveItem function.

If it does not return / print anything just check what data your sending the server and you should be good.

I did some further testing and turns out
the function just skips the first item in the folder for no reason, I don’t know why or how, This hasn’t been an issue until now, and it doesn’t explain why it works sometimes