Code issues - Play animation having tool and mouse 1 down

Hi, I need to know what is wrong with my code and how to fix it.

I would like that when pressing the ‘mouse button 1 down’ it reproduces the animation with the bat only 1 time, how do I do that?

Localscript:

-- Animations
local hitball = Instance.new("Animation")
--local holdbat = Instance.new("Animation")

local Animator = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid"):WaitForChild("Animator")
hitball.AnimationId = "rbxassetid://6307821883" -- hit the ball anim
--holdbat.AnimationId = "rbxassetid://" -- hold bat anim
---------------------------------------------------------------------


    local tool = script.Parent
    local player = game:GetService("Players").LocalPlayer

    if workspace.FilteringEnabled then
    	tool.Activated:Connect(function()
    		local mouse = player:GetMouse()
    		mouse.Button1Down:Connect(function()
    			Animator:LoadAnimation(hitball):Play()
    			tool.RemoteEvent:FireServer(mouse.hit.p)
    		end)
    	end)
    end

ServerScript:

local tool = script.Parent
tool.RemoteEvent.OnServerEvent:Connect(function(player)
	
end)


script.Parent.Equipped:Connect(function()
	
	
	
end)

Images:

image

image

2 Likes

Well, fifrst of all delete the “if workspace.FilteringEnabled” statement because that doesn’t make sense, as all games nowadays are pretty much Filtering Enabled. Secondly, I recommend you use UserInputService instead of mouse, as Mouse is pretty much deprecated and UIS has more advantages. Also, if you are using tool.Activated, you dont need mouse.Button1Down, so yeah.

But if I want to reproduce the animation when pressing button 1 of the mouse only when this tool is activated, how do I do it?

Tool.Activated already acts as Mouse.ButtonDown, as Tool.Activated fires when you press Leftclick, so theres no need to use Mouse.ButtonDown.
The Activated event fires when the player clicks while a Tool is equipped.

1 Like

Oh I had no idea about that! I’m going to try and tell you how it works for me :scream:

try doing

MouseButton1Down

I have an error in the localscript with this part …

Ok, that error has nothing to do with the actual equipping part, its that the Humanoid doesnt load before you actually define Animator. Try doing this:
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild(“Humanoid”)
local Animator = Humanoid:WaitForChild(“Animator”)

Also, make sure that Animator actually exists. Try printing Animator.Parent or Animator.

Currently, every time they activate the tool you make another connection, so if they activate the tool 10 times then press down it will fire 10 times. Whenever the tool is activated, the tool must be equipped so you can just do this:

 local tool = script.Parent
    local player = game:GetService("Players").LocalPlayer

    if workspace.FilteringEnabled then
    	tool.Activated:Connect(function()
    		local mouse = player:GetMouse()
    			Animator:LoadAnimation(hitball):Play()
    			tool.RemoteEvent:FireServer(mouse.hit.p)
    	end)
    end

I already fixed him that, I think theres no need to write a whole code. (Look in the upper posts.)

I just did this and the animation does not play, what is the error? :frowning:

– Animations

local hitball = Instance.new("Animation")

--local holdbat = Instance.new("Animation")

local player = game.Players.LocalPlayer

local Character = player.Character or player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")

local Animator = Humanoid:WaitForChild("Animator")

local Animator = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid"):WaitForChild("Animator")

hitball.AnimationId = "rbxassetid://6307821883" -- hit the ball anim

--holdbat.AnimationId = "rbxassetid://" -- hold bat anim

---------------------------------------------------------------------

local tool = script.Parent

local player = game:GetService("Players").LocalPlayer

if workspace.FilteringEnabled then

tool.Activated:Connect(function()

local mouse = player:GetMouse()

Animator:LoadAnimation(hitball):Play()

tool.RemoteEvent:FireServer(mouse.hit.p)

end)

end

What does the output print out. And I also said remove the “workspace.FilteringEnabled” because its useless. Btw, you dont need to reference player twice.

Secondly, I really recommend roblox developerhub instead of developerforum, as its a official website made by Roblox which has opensource code and documented API and pretty much everything. Id suggest you go to the Animation roblox hub API and take a look at the open source code.

I just removed that and removed the double reference to the player, and the output prints me that it is connected… the problem is that it does not reproduce the animation

script

local event = script.Parent.RemoteEvent

event.OnServerEvent:Connect(function()
	print("connected.")
end)

localscript:

local hitball = Instance.new("Animation")

--local holdbat = Instance.new("Animation")

local player = game.Players.LocalPlayer

local Character = player.Character or player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")

local Animator = Humanoid:WaitForChild("Animator")

local Animator = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid"):WaitForChild("Animator")

hitball.AnimationId = "rbxassetid://6307821883" -- hit the ball anim

--holdbat.AnimationId = "rbxassetid://" -- hold bat anim

---------------------------------------------------------------------

local tool = script.Parent

tool.Activated:Connect(function()

Animator:LoadAnimation(hitball):Play()

tool.RemoteEvent:FireServer()

end)

I just solved it !! :smiley: thank you all really

--- tools

script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
		animation:Play()
	end)
end)

script.Parent.Unequipped:Connect(function()
	animation:Stop()
end)