Basically, I’m trying to make an NPC that teaches the player how to drop items. Once you get to the point where he tells you to press backspace, I have 2 events depending on whether the player is holding the item or not. The problem is, even if I did hold the item before, he still doesn’t detect that I did. Here’s what I’m talking about:
I’m pretty sure the problem here is that the item gets unequipped before the script can check if it was equipped or not, but I have no idea how to fix this. Here’s my code:
key.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Backspace then
local bloxy = player.Backpack:FindFirstChild("Bloxy Cola")
if not bloxy then
--item was equipped
else
--item wasn't equipped
end
end
end)
I also tried to fix this by first checking if the item was equipped and then checking if the pressed key was Backspace, but it still didn’t work. Also, the catch here is that you can’t drop items, so checking if the item is in Workspace won’t work either.
It would probably be more accurate if instead of using
local bloxy = player.Backpack:FindFirstChild("Bloxy Cola")
U used :
local bloxy = player.Character and player.Character:FindFirstChild("Bloxy Cola")
EDIT :
You cannot check if player has the item equipped when the key is pressed since the InputBegan event fire after the player drop the item. Instead you should verify before InputBegan so it’ll know if the player equipped the item :
local wasEquipped = false
local inputBeganConnection = nil
local function onEquipped()
wasEquipped = true
inputBeganConnection = key.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Backspace then
if wasEquipped then
-- item was equipped
else
-- item wasn't equipped
end
end
end)
end
local function onAncestryChanged()
if tool.Parent == plr.Character then return end
if inputBeganConnection then
inputBeganConnection:Disconnect()
end
end
tool.AncestryChanged:Connect(onAncestryChanged)
tool.Equipped:Connect(onEquipped)
Then you can adapt this piece of program to solve ur problem
Okay, I solved this myself now, but thanks for helping either way. I made a seperate script to keep track of whether the item is equipped or not and change a BoolValue to true/false, and then I edited my current script to check the value instead of where the tool is. Since the loop checking the item has a 0.2 second delay, the other script can check the value before it changes to false.