You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want a mop tool to play an animation when I press the E key, but only when the mop is in my hand.
What is the issue? Include screenshots / videos if possible!
The script works perfectly up until the point when I hit the E key without the mop tool out. The animation plays regardless of the mop being in my hand
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried making the animation play only if the tool.Equiped event had been fired but it does not seem to work.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that itâs easier for people to help you!
local AnimationEvent = game.ReplicatedStorage.Mopping -- An event that is fired when the key is pressed.
local deBounce = false
local UIS = game:GetService("UserInputService")
local tool = script.Parent
local CleanRad = tool:WaitForChild("CleanRad") -- A small box around the mop to detect if it is touching a dirty spot on the floor.
local mopping = tool:WaitForChild("Mopping") -- A boolean for future use.
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E and deBounce == false and tool.Equipped then
mopping.Value = true
CleanRad.CanTouch = true
AnimationEvent:FireServer()
deBounce = true
wait(1)
CleanRad.CanTouch = false
mopping.Value = false
deBounce = false
end
end
end)
Please do not ask people to write entire scripts or design entire systems for you. If you canât answer the three questions above, you should probably pick a different category.
You can store a var called IsEquipped and set it to true when the tool isEquipped and set it to false when itâs Unequipped and check if itâs true before playing the animation
This solution did not seem to work for me, it still played the animation without the tool equipped, here is the code that I wrote:
local AnimationEvent = game.ReplicatedStorage.Mopping -- An event that is fired when the key is pressed.
local deBounce = false
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer.Character
local tool = script.Parent
local CleanRad = tool:WaitForChild("CleanRad") -- A small box around the mop to detect if it is touching a dirty spot on the floor.
local mopping = tool:WaitForChild("Mopping") -- A boolean for future use.
local isEquipped = tool:WaitForChild("isEquipped")
if tool.Equipped then
isEquipped.Value = true
elseif tool.Unequipped then
isEquipped.Value = false
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if isEquipped.Value == true and input.KeyCode == Enum.KeyCode.E and deBounce == false then
mopping.Value = true
CleanRad.CanTouch = true
AnimationEvent:FireServer()
deBounce = true
wait(1)
CleanRad.CanTouch = false
mopping.Value = false
deBounce = false
end
end
end)
local AnimationEvent = game.ReplicatedStorage.Mopping â An event that is fired when the key is pressed.
local deBounce = false
local UIS = game:GetService(âUserInputServiceâ)
local player = game.Players.LocalPlayer.Character
local tool = script.Parent
local CleanRad = tool:WaitForChild(âCleanRadâ) â A small box around the mop to detect if it is touching a dirty spot on the floor.
local mopping = tool:WaitForChild(âMoppingâ) â A boolean for future use.
local isEquipped = false
if tool.Equipped then
isEquipped = true
elseif tool.Unequipped then
isEquipped = false
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if isEquipped.Value == true then
if input.KeyCode == Enum.KeyCode.E and deBounce == false then
mopping.Value = true
CleanRad.CanTouch = true
AnimationEvent:FireServer()
deBounce = true
wait(1)
CleanRad.CanTouch = false
mopping.Value = false
deBounce = false
end
end
end)
or
local AnimationEvent = game.ReplicatedStorage.Mopping â An event that is fired when the key is pressed.
local deBounce = false
local UIS = game:GetService(âUserInputServiceâ)
local player = game.Players.LocalPlayer.Character
local tool = script.Parent
local CleanRad = tool:WaitForChild(âCleanRadâ) â A small box around the mop to detect if it is touching a dirty spot on the floor.
local mopping = tool:WaitForChild(âMoppingâ) â A boolean for future use.
if tool.Equipped then
isEquipped = true
elseif tool.Unequipped then
isEquipped = false
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if script:FindFirstAncestor(âHumanoidâ) then
if input.KeyCode == Enum.KeyCode.E and deBounce == false then
mopping.Value = true
CleanRad.CanTouch = true
AnimationEvent:FireServer()
deBounce = true
wait(1)
CleanRad.CanTouch = false
mopping.Value = false
deBounce = false
end
end
end)
This solution may have worked, but I now have a new error message that wonât let me play the animation at all. it says : âPlayers.narfrich.Backpack.Mop.MoppingScript:19: attempt to index boolean with âValueââ
Here is the code:
local AnimationEvent = game.ReplicatedStorage.Mopping
local deBounce = false
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer.Character
local tool = script.Parent
local CleanRad = tool:WaitForChild("CleanRad")
local mopping = tool:WaitForChild("Mopping")
local isEquipped = false
if tool.Equipped then
isEquipped = true
elseif tool.Unequipped then
isEquipped = false
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if isEquipped.Value == true then
if input.KeyCode == Enum.KeyCode.E and deBounce == false then
mopping.Value = true
CleanRad.CanTouch = true
AnimationEvent:FireServer()
deBounce = true
wait(1)
CleanRad.CanTouch = false
mopping.Value = false
deBounce = false
end
end
end
end)
local AnimationEvent = game.ReplicatedStorage.Mopping
local deBounce = false
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer.Character
local tool = script.Parent
local CleanRad = tool:WaitForChild("CleanRad")
local mopping = tool:WaitForChild("Mopping")
local isEquipped = false
if tool.Equipped then
isEquipped = true
elseif tool.Unequipped then
isEquipped = false
end
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if isEquipped == true then
if input.KeyCode == Enum.KeyCode.E and deBounce == false then
mopping.Value = true
CleanRad.CanTouch = true
AnimationEvent:FireServer()
deBounce = true
wait(1)
CleanRad.CanTouch = false
mopping.Value = false
deBounce = false
end
end
end
end) ```
This worked thank you! But after using print() to check my code I am now realizing that the tool.Equipped event checks if the tool is in a playerâs backpack, or the toolbar. I am still unsure how to check if the tool is in hand or not.
After checking the API, I realized that I was not using functions to my advantage, I have fixed my code!
here it is:
local AnimationEvent = game.ReplicatedStorage.Mopping
local deBounce = false
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer.Character
local tool = script.Parent
local CleanRad = tool:WaitForChild("CleanRad")
local mopping = tool:WaitForChild("Mopping")
local isEquipped = false
local function onEquipped(_mouse)
isEquipped = true
print("The tool was equipped")
end
tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(function()
isEquipped = false
print("The tool was unequipped")
end)
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if isEquipped == true then
if input.KeyCode == Enum.KeyCode.E and deBounce == false then
mopping.Value = true
CleanRad.CanTouch = true
AnimationEvent:FireServer()
deBounce = true
wait(1)
CleanRad.CanTouch = false
mopping.Value = false
deBounce = false
end
end
end
end)