Why does the local function still set players health to 0?

Hi! I have a local function in my script. Its supposed to make you stay alive if you have one of the items from the BaggagesColors. If you dont have one of these items, it sets your health on 0. But somehow I cant figure out why it still sets your health on 0 when you have one of the items. I tried a few ways to fix this like: table.find, if commands and more. Can somebody help me with this local function?

local function checkIfPlayerHasBaggage()
	local BaggagesColors = {"BlackBaggage","BlueBaggage","BrownBaggage","DarkblueBaggage","GrayBaggage","GreenBaggage","OrangeBaggage","PinkBaggage","RedBaggage","VioletBaggage","WhiteBaggage","YellowBaggage"}

	for i,v in pairs(game.Players:GetPlayers()) do
		local Baggages = v.Backpack
		local Haveitem = false
		for _, color in pairs(BaggagesColors) do
			if Baggages:FindFirstChild(color) then
				Haveitem = true
				else
					v.Character.Humanoid.Health = 0
				end
			end
		end
	end

If the player has the tool equipped, it will be parented inside of the Character, not Backpack. Also, make sure your capitalization and spelling is correct.

The reason is because you are checking every color you did set “Haveitem” variable to true but you also continue to check if player have other color if they don’t pass condition on next color it will kill them (this mean they need to have all color to survive)(and sorry if you confused i am not so good at explain)

here is the edited version

local function checkIfPlayerHasBaggage()
	local BaggagesColors = {"BlackBaggage","BlueBaggage","BrownBaggage","DarkblueBaggage","GrayBaggage","GreenBaggage","OrangeBaggage","PinkBaggage","RedBaggage","VioletBaggage","WhiteBaggage","YellowBaggage"}

	for i,v in pairs(game.Players:GetPlayers()) do
		local Baggages = v.Backpack
		local Haveitem = false
		for _, color in pairs(BaggagesColors) do
			if Baggages:FindFirstChild(color) then
				Haveitem = true
			end
		end
		
		if not Haveitem then
			v.Character.Humanoid.Health = 0
		end
		
	end
end
1 Like

it works, but is there a way to see if the item is in the charakter? Because if you hold the item in your hand, the item goes to the charakter and isnt in the backpack anymore. And also thanks for helping me with the script.

You can make reference to player character and find it from there like this

for i,v in pairs(game.Players:GetPlayers()) do
		
		local character = v.Character
		local Baggages = v.Backpack
		local Haveitem = false
		for _, color in pairs(BaggagesColors) do
			if Baggages:FindFirstChild(color) or character:FindFirstChild(color) then
				Haveitem = true
			end
		end
		
		if not Haveitem then
			v.Character.Humanoid.Health = 0
		end
		
	end
1 Like