What do you want to achieve?
I am trying to make a gladius play a swinging animation when I click with it.
What is the issue?
The animation won’t play.
Here is my code:
local Humanoid = script.Parent.Character.Humanoid
local Animation = Humanoid:LoadAnimation(script:WaitForChild("Animation"))
local debounce = false
script.Parent.Parent.Activated:Connect(function()
if not debounce then
debounce = true
Animation:Play()
wait(0.5)
Animation:Stop()
debounce = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
Humanoid.Health = Humanoid.Health - 15
end
end)
end
end)
The script is a server script inside of the tool’s Handle and the animation is the child of the script.
The character may have not loaded yet, same with the humanoid and disconnect the event after the animation has ended so you can’t kill others while your not clicking.
If your game is created by a group can the animation is created by you then upload the animation onto the group.
And put your Animation priority to Action.
local Player = script.Parent
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("Animation"))
local debounce = false
script.Parent.Parent.Activated:Connect(function()
if not debounce then
debounce = true
local connection = script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
Humanoid.Health = Humanoid.Health - 15
end
end)
Animation:Play()
Animation.Stopped:Wait()
connection:Disconnect()
debounce = false
end
end)
Try this and make sure your character Rig type match the Animation because the Roblox Slash Animation is R6
local Humanoid = script.Parent.Parent.Parent:FindFirstChildWhichIsA("Humanoid")
local debounce = false
--
script.Parent.Parent.Activated:Connect(function()
local Humanoid = script.Parent.Parent.Parent:FindFirstChildWhichIsA("Humanoid")
local Animation = Humanoid:LoadAnimation(script:FindFirstChildWhichIsA("Animation"))
if not debounce then
debounce = true
Animation:Play()
wait(0.5)
Animation:Stop()
debounce = false
--
script.Parent.Touched:Connect(function(hit)
local HitHumanoid = hit.Parent:FindFirstChild("Humanoid")
if hit.Parent.Name ~= Humanoid.Parent.Name and HitHumanoid then
HitHumanoid:TakeDamage(15)
end
end)
end
end)
That obviously wouldn’t work because you have a server script inside the tool, server can’t read client’s inputs, so instead make that script a LocalScript, and avoid loading the animation to the Humanoid, it’s highly unrecommended, just load it to the animator.
The Animator instance is parented to the Humanoid instance on each player character, so it’s just local animator = Humanoid.Animator and then load it the same way you are doing for humanoid rn.
local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local Animator = Humanoid.Animator
local Animation = Animator:LoadAnimation(script:WaitForChild("Animation"))
local debounce = false
script.Parent.Activated:Connect(function()
if not debounce then
debounce = true
Animation:Play()
wait(0.5)
Animation:Stop()
debounce = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
Humanoid.Health = Humanoid.Health - 15
end
end)
end
end)
It gives me this error: 13:29:31.805 Players.MRBOGO_YT.Backpack.Gladius.SwordScript:1: attempt to index nil with 'Humanoid' - Client - SwordScript:1
You first need to change the script from a ServerScript to a LocalScript because ServerScripts cant read client’s inputs, your next problem is that you aren’t waiting for those instances to show up, aka the Animator doesn’t exist yet, so you’ll want to WaitForChild() there.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local Animation = Animator:LoadAnimation(script:WaitForChild("Animation"))