How can I detect if a player has a specific item in their inventory

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!
    Im building a sort of side quest to my game where you need to give 3 donuts to a wizard
    that he will convert into a “Poisoned Donut”

However, I need to find a way to see if they do have the 3 donuts
and If they only bring 1, he says
"I still need you to bring (donut) and (donut)
and if they bring 2
"I still need you to bring the (donut)

i dont whats wrong with the script, but even when i have 2 of the needed donuts in my inv,
it only fires once ( i only lose 1 donut)

if talkedtowizard == true then 
			

		if npc.Name == "Wizard" then
		local RequiredDonuts = {
			game.ReplicatedStorage.Tools:WaitForChild('Donuts'):WaitForChild('Chocolate Donut').Name,
			game.ReplicatedStorage.Tools:WaitForChild('Donuts'):WaitForChild('OrangeCherry Donut').Name,
			game.ReplicatedStorage.Tools:WaitForChild('Donuts'):WaitForChild('WaterMelon Donut').Name
		}
	
		for i, donut in ipairs(RequiredDonuts) do
			if plr.Character:FindFirstChild(donut) or plr.Backpack:FindFirstChild(donut) then
			
			print('brought ' .. donut)
			game.ReplicatedStorage.remotes.deletedonut:FireServer(donut)
			table.remove(RequiredDonuts, table.find(RequiredDonuts, donut))
			end
		end
		print(RequiredDonuts)
		if #RequiredDonuts == 0 then
			TextLines = {
				'thank you',
				
			}
		else
			if #RequiredDonuts == 1 then
			TextLines = {
				"Thank you,",
				"but I still need a " .. RequiredDonuts[1],
				'Come back when you have it.'
			}					
			elseif #RequiredDonuts == 2 then
				TextLines = {
				"Thank you, but you still need to bring:",
			" a " ..	RequiredDonuts[1],
			" a " ..	RequiredDonuts[2],
				}
				
				

			end
		end
		
		end
		end

Why don’t you just do smth like this

local donuts = 0

-- Goes through the backpack (will only get unequipped tools)
for i, v in pairs(player.Backpack:GetChildren()) do
	if v.Name == "Donut" then -- Check if its name is donut (you can change if you want another way to detect a donut)
		donuts += 1
	end
end

-- Goes through the character (will only detect equipped tools)
for i, v in pairs(player.Character:GetChildren()) do
	if v.Name == "Donut" then -- Check if its name is donut (you can change if you want another way to detect a donut)
		donuts += 1
	end
end

if donuts == 1 then -- If one donut
	-- Code
elseif donuts == 2 then -- If two donuts
	-- Code
elseif donuts >= 3 then -- If three donuts or more
	-- Code
end

how do i know what donuts still havent been given tho

Try this. Inside the script, add a NumberValue

then try this code here

if talkedtowizard == true then 

	if npc.Name == "Wizard" then
		local RequiredDonuts = script.NumberValue.Value
		
		for i, donut in ipairs(plr.Backpack:GetChildren()) do
			if donut.Name == "Donut" then
				game.ReplicatedStorage.remotes.deletedonut:FireServer(donut)
				RequiredDonuts -= 1
			end
		end
		print(RequiredDonuts)
		if #RequiredDonuts == 0 then
			TextLines = {
				'thank you',

			}
		else
			if #RequiredDonuts == 1 then
				TextLines = {
					"Thank you,",
					"but I still need " .. RequiredDonuts .. "more",
					'Come back when you have it.'
				}					
			elseif #RequiredDonuts == 2 then
				TextLines = {
					"Thank you, but you still need to bring" .. RequiredDonuts .. "more donuts",
				}

			end
		end
	end
end
1 Like