Output is contradicting Itself?

I have a function that checks a parameter passed from a local script against folders in the players directory. If it finds it it edits the Items quantity by -1. However the output states that the folder has been found yet I get the output that the item doesn’t exist? I’m not sure if I’m just mindlessly overlooking something or not.

Here’s the output

  17:01:31.702  LUFCarebest's PurchasedSpells folder contains:  -  Server - Newinvmanager:79
  17:01:31.703   - spell1  -  Server - Newinvmanager:81
  17:01:31.703   - spell2  -  Server - Newinvmanager:81
  17:01:31.703   - spell3  -  Server - Newinvmanager:81
  17:01:31.703   - spell4  -  Server - Newinvmanager:81
  17:01:31.703   - spell5  -  Server - Newinvmanager:81
  17:01:31.703   - spell8  -  Server - Newinvmanager:81
  17:01:31.704  LUFCarebest tried to use an item that doesn't exist: spell1   -  Server - Newinvmanager:122
  17:01:34.510  Equiping item: spell2   -  Client - KitSelect:88
  17:01:34.536  LUFCarebest's PurchasedSpells folder contains:  -  Server - Newinvmanager:79
  17:01:34.536   - spell1  -  Server - Newinvmanager:81
  17:01:34.536   - spell2  -  Server - Newinvmanager:81
  17:01:34.536   - spell3  -  Server - Newinvmanager:81
  17:01:34.536   - spell4  -  Server - Newinvmanager:81
  17:01:34.536   - spell5  -  Server - Newinvmanager:81
  17:01:34.537   - spell8  -  Server - Newinvmanager:81
LUFCarebest tried to use an item that doesn't exist: spell2   -  Server - Newinvmanager:122
local function handleUse(player, itemName)
	local spellFolder = player:FindFirstChild("PurchasedSpells")

	-- If the spell folder exists, we need to search for the item inside it
	if spellFolder then
		-- Debugging: Print the contents of the PurchasedSpells folder
		print(player.Name .. "'s PurchasedSpells folder contains:")
		for _, item in pairs(spellFolder:GetChildren()) do
			print(" - " .. item.Name)  -- Print all items in the folder
		end

		local spellItem = nil

		-- Search through the subfolders for a StringValue called "Name"
		for _, subfolder in pairs(spellFolder:GetChildren()) do
			if subfolder:IsA("Folder") then
				local nameValue = subfolder:FindFirstChild("Name")
				if nameValue then
					print("Checking spell: " .. nameValue.Value)  -- Debug print
					if nameValue.Value == itemName then
						spellItem = subfolder -- Set spellItem to the whole folder
						break -- Exit loop if the item is found
					end
				else
					print("No Name value found in folder: " .. subfolder.Name) -- Debug print for missing Name
				end
			end
		end

		-- Now check if the spellItem was found
		if spellItem then
			local quantityValue = spellItem:FindFirstChild("Quantity")
			if quantityValue then
				if quantityValue.Value > 0 then
					quantityValue.Value = quantityValue.Value - 1
					print(player.Name .. " used 1 of " .. itemName .. ". Remaining: " .. quantityValue.Value)

					-- If quantity reaches zero, remove the item from the player's spells
					if quantityValue.Value <= 0 then
						spellItem:Destroy()
						print(player.Name .. " has no more " .. itemName .. " left, removing it.")
					end
				else
					warn(player.Name .. " tried to use " .. itemName .. " but quantity is 0.")
				end
			else
				warn("Quantity value is missing for item: " .. itemName)
			end
		else
			warn(player.Name .. " tried to use an item that doesn't exist: " .. itemName)
		end
	else
		warn(player.Name .. " does not have any PurchasedSpells folder.")
	end
end


Try replacing this section (I added a print for debugging), chances are the nameValue’s value isn’t the same as the itemName, so although it finds the correct nameValue instance its still doesn’t set the spellItem correctly.

for _, subfolder in pairs(spellFolder:GetChildren()) do
			if subfolder:IsA("Folder") then
				local nameValue = subfolder:FindFirstChild("Name")
				if nameValue then
					print("Checking spell: " .. nameValue.Value)  -- Debug print
					if nameValue.Value == itemName then
						spellItem = subfolder -- Set spellItem to the whole folder
						break -- Exit loop if the item is found
                                        else
                                             print("nameValue value is:", nameValue.Value, "and itemName is:", itemName)
					end
				else
					print("No Name value found in folder: " .. subfolder.Name) -- Debug print for missing Name
				end
			end
		end
1 Like

Found the issue, basically the parameter being passed was "Spell1 " not “Spell1” It had a space so i create a trim function to remove the whitespace and it works now

2 Likes

I’ve now got a similar issue though and it doesnt have anything to do with whitespace

 LUFCarebest does not have any PurchasedEmotes folder.  -  Server - Newinvmanager:101
  21:00:15.652  LUFCarebest's children:  -  Server - Newinvmanager:103
  21:00:15.652   - StarterGear  -  Server - Newinvmanager:105
  21:00:15.652   - PlayerGui  -  Server - Newinvmanager:105
  21:00:15.652   - PurchasedEmotes  -  Server - Newinvmanager:105
  21:00:15.653   - PurchasedSpells  -  Server - Newinvmanager:105
  21:00:15.653   - Data  -  Server - Newinvmanager:105
  21:00:15.653   - leaderstats  -  Server - Newinvmanager:105
  21:00:15.653   - Backpack  -  Server - Newinvmanager:105
-- Function to handle using an item (Emote or Spell)
local function handleUse(player, itemName, itemType)
	print("Attempting to use item: " .. itemName) -- Debugging line

	local folder

	-- Identify whether it's a Spell or an Emote
	if itemType == "Spell" then
		folder = player:FindFirstChild("PurchasedSpells")
		if not folder then
			folder = player:WaitForChild("PurchasedSpells", 5) -- Wait for the folder to exist
		end
		print(player.Name .. " is attempting to use a spell.")
	elseif itemType == "Emote" then
		folder = player.PurchasedEmotes
		if not folder then
			folder = player:WaitForChild("PurchasedEmotes", 5) -- Wait for the folder to exist
		end
		print(player.Name .. " is attempting to use an emote.")
	end

	-- DEBUGGING: Print the folder state
	if not folder then
		warn(player.Name .. " does not have any " .. (itemType == "Spell" and "PurchasedSpells" or "PurchasedEmotes") .. " folder.")
		-- Print the player's immediate children to check if PurchasedEmotes exists
		print(player.Name .. "'s children:")
		for _, child in pairs(player:GetChildren()) do
			print(" - " .. child.Name)  -- This will print the player's direct children
		end
		return
	else
		print("Found folder: " .. folder.Name)
	end

	local itemToUse = folder:FindFirstChild(itemName)

	-- DEBUG: Check if the item is found in the folder
	if not itemToUse then
		warn(player.Name .. " tried to use an item that doesn't exist: " .. itemName)
		return
	else
		print("Found item: " .. itemToUse.Name)
	end

	-- If the item is found, proceed with using it
	local quantityValue = itemToUse:FindFirstChild("Quantity")
	if quantityValue then
		if quantityValue.Value > 0 then
			quantityValue.Value = quantityValue.Value - 1
			print(player.Name .. " used 1 of " .. itemName .. ". Remaining: " .. quantityValue.Value)

			-- Fire the update event to the player for UI updates
			updateItemQuantityEvent:FireClient(player, itemName, quantityValue.Value)

			if quantityValue.Value <= 0 then
				itemToUse:Destroy()
				print(player.Name .. " has no more " .. itemName .. " left, removing it.")
			end
		else
			warn(player.Name .. " tried to use " .. itemName .. " but quantity is 0.")
		end
	else
		warn("Quantity value is missing for item: " .. itemName)
	end
end

Check the itemType parameter is being passed correctly. It must be the same as “Spell” or “Emote” for the folder variable to be assigned a value.
(Just add the itemType to the debugging print line to check)

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