My script is an eating script where after a certain amount of clicks, the item goes away.
The issue with the script is, if you have multiple of the same food item in your backpack, whenever you click, the IntValues are reacting very randomly, sometimes it takes 3 off the value of the other item, sometimes 4 off of the current item. It’s very odd, and I’m not sure why.
local Tool = script.Parent
local EatAnimation = Tool.EatAnimation
local BitesLeft = Tool.BitesLeft
Tool.Equipped:Connect(function(Mouse)
Tool.Activated:Connect(function()
if script.Parent.BitesLeft.Value > 0 then
local Character = Tool.Parent
local AnimationTrack = Character.Humanoid:LoadAnimation(EatAnimation)
AnimationTrack:Play()
script.Parent.BitesLeft.Value = script.Parent.BitesLeft.Value - 1
end
if script.Parent.BitesLeft.Value == 0 then
Tool:Destroy()
end
end)
end)
Could anyone help out on why the IntValues are like this? It only happens if there is more then one of the same item in your inventory.
Because you have the Tool.Activated event within the Tool.Equipped event, it is setting up a new event listener to Activated every time the Tool is Equipped (this is why it would take off 3 from the value after being Activated, if you have equipped the tool 3 times previously). I believe Tool.Activated will only fire when the Tool is Equipped so you can just take that event out of the Tool.Equipped event.
If Tool.Activated is removed, the animation only plays when the tool is equipped, not when it’s clicked. It does remove the issue where more then 1 value is removed from the Value.
I meant keep the line for the Activated event and delete the Equipped:
Tool.Activated:Connect(function()
if script.Parent.BitesLeft.Value > 0 then
local Character = Tool.Parent
local AnimationTrack = Character.Humanoid:LoadAnimation(EatAnimation)
AnimationTrack:Play()
script.Parent.BitesLeft.Value = script.Parent.BitesLeft.Value - 1
end
if script.Parent.BitesLeft.Value == 0 then
Tool:Destroy()
end
end)