Script not working properly

Hello there! I’ve a script that is a event listener. It works all good but there is a line that doesn’t work properly.

ReplicatedStorage:WaitForChild("Remotes").AppleEvent:FindFirstChild("PlayerHealthGain").OnServerEvent:Connect(function(player)
	     if not debounce then
		        debounce = true 
		        player.Character:WaitForChild("Humanoid").MaxHealth = player.Character:WaitForChild("Humanoid").MaxHealth + 5
		        player.Character:WaitForChild("Humanoid").Health = player.Character:WaitForChild("Humanoid").Health + 1
		        local Apple = player.Character:FindFirstChild("Apple") or player.Backpack:FindFirstChild("Apple") or player.StarterGear:FindFirstChild("Apple")
		        if Apple then
			    Apple:Destroy()
		end
		        wait(1)
		        debounce = false
	    end
end)

If I remove player.StarterGear:FindFirstChild(“Apple”), then it will work but why? I want it to get destroyed from the StarterGear as well.

I don’t think you should have all of these 3 locations dependent on one variable. It’s possible for Apple to be located in the Backpack or Character and at the same time available in StarterGear.

local IsAppleInInventory = player.Character:FindFirstChild("Apple") or player.Backpack:FindFirstChild("Apple")
local IsAppleStarterGear = player.StarterGear:FindFirstChild("Apple")

if IsAppleInInventory then
    IsAppleInInventory:Destroy()
end

if IsAppleStarterGear then
    IsAppleStarterGear:Destroy()
end
1 Like

Can you be more clear? I don’t understand what you want to accomplish

1 Like

Ah, alright! Thanks. (30 chars)