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)
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.
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.
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”)
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 just did this and the animation does not play, what is the error?
– 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)