Playing Animations only when a tool is in hand

You can write your topic however you want, but you need to answer these questions:

  1. 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.
  2. 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
  3. 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.

2 Likes

I’d just look if the mop is a child of the character.

2 Likes

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

1 Like

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)

1 Like

Hold on let me write something for you and tell me if it doesn’t work

1 Like

This should work

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)

1 Like

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)

Should be

(Sorry for my chat being broken right now)


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.

2 Likes

You could run a check using

if script:FindFirstAncestor(“Humanoid”) then

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)
2 Likes

Hit me up if you need any more help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.