When i try to play an animation once a player activates a tool, it does not play

when the player activates the tool it fires a remote event that leads into a server script where then once the server script receives the event it choose a random animation out of three animations and plays it.

but when i test it, it doesn’t seem to be working.

there is no errors.

local Tool = script.Parent
local Handle = Tool.Handle
local Anims = Tool.Anims

local RaycastHitbox = require(game.ServerScriptService.RaycastHitboxV4)
local Hitbox = RaycastHitbox.new(Tool.Hitbox)

local de = false
local canHit = false

config = {
	Damage = 15,
	AttackTime = .25,
	Cooldown = .25
}

Hitbox.OnHit:Connect(function(hit, humanoid)
	if canHit == false then return end
	canHit = false
	if humanoid.Parent ~= script.Parent.Parent then
		humanoid:TakeDamage(config.Damage)
		
		local character = humanoid.Parent
		
		local Knockback = Instance.new("BodyVelocity")
		Knockback.P = math.huge
		Knockback.Parent = character:FindFirstChild("HumanoidRootPart")
		Knockback.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		Knockback.Velocity = character:FindFirstChild("HumanoidRootPart").CFrame.lookVector * -7
		game:GetService("Debris"):AddItem(Knockback, .3)
		task.wait(.35)
	end
	canHit = true
end)

script.Parent.Hit.OnServerEvent:Connect(function(Player)
	if de == false then
		de = true
		local Humanoid = Player.Character.Humanoid
		local AnimationsList = Anims:GetChildren()
		local Selected = Humanoid:LoadAnimation(AnimationsList[math.random(1, #AnimationsList)])
		Selected:Play()
		
		Hitbox:HitStart()
		wait(config.AttackTime)
		canHit = true
		wait(config.Cooldown)
		Hitbox:HitStop()
		canHit = false
		de = false
	end
end)
1 Like

Try playing animations on the client instead of the server, animations are replicated.

whats the point of that, i want everyone to see the animation.

When you play an animation on the client then it will be replicated into other clients. Based off of this documentation: https://create.roblox.com/docs/reference/engine/classes/Animation

tried the same thing on the client just now, still did not work.

Is this script running on the client or server?

its running on the client. its in a local script

I think i see the problem

script.Parent.Hit.OnServerEvent:Connect(function(Player)

for this line, it is supposed to be OnClientEvent instead of OnServerEvent

try changing it

why, that line is in a server script. theres two scripts local and server scripts in the tool. the local script just checks when the tool is activated and then fires an event.

oh i thought it was a single script, lemme continue looking into it

Few ideas cross to my mind…

  1. Use wait() Wisely:
  • Instead of using wait() to control timing, consider using RunService for more precise timing and better responsiveness.
  1. LoadAnimation Error Handling:
  • Ensure that the LoadAnimation call is successful. If the selected animation fails to load, it could be a source of the problem.
  1. Check the RemoteEvent:
  • Confirm that the RemoteEvent (script.Parent.Hit.OnServerEvent) is set up correctly on the client side and is being fired appropriately.

And so my rescript version probably looks like this?

local Tool = script.Parent
local Anims = Tool.Anims

local RunService = game:GetService("RunService")
local RaycastHitbox = require(game.ServerScriptService.RaycastHitboxV4)
local Hitbox = RaycastHitbox.new(Tool.Hitbox)

local de = false
local canHit = false

local config = {
    Damage = 15,
    AttackTime = 0.25,
    Cooldown = 0.25
}

Hitbox.OnHit:Connect(function(hit, humanoid)
    if canHit == false then return end
    canHit = false
    
    if humanoid.Parent ~= Tool.Parent then
        humanoid:TakeDamage(config.Damage)
        
        local character = humanoid.Parent
        
        local Knockback = Instance.new("BodyVelocity")
        Knockback.P = math.huge
        Knockback.Parent = character:FindFirstChild("HumanoidRootPart")
        Knockback.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
        Knockback.Velocity = character:FindFirstChild("HumanoidRootPart").CFrame.lookVector * -7
        game:GetService("Debris"):AddItem(Knockback, 0.3)
        
        wait(0.35)
    end
    
    canHit = true
end)

script.Parent.Hit.OnServerEvent:Connect(function(Player)
    if de == false then
        de = true
        local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
        
        if Humanoid then
            local AnimationsList = Anims:GetChildren()
            if #AnimationsList > 0 then
                local Selected = Humanoid:LoadAnimation(AnimationsList[math.random(1, #AnimationsList)])
                
                if Selected then
                    Selected:Play()
                    
                    Hitbox:HitStart()
                    wait(config.AttackTime)
                    canHit = true
                    wait(config.Cooldown)
                    Hitbox:HitStop()
                    canHit = false
                else
                    warn("Failed to load animation.")
                end
            else
                warn("No animations found.")
            end
        else
            warn("Humanoid not found.")
        end
        
        de = false
    end
end)

What you think?

i tried that and nothing changed. But i think i found the problem. I changed the animation to mvoe the leg instead and it worked. but when the animation is to move the arm holding the tool it doesn’t play.

i dont know how this would be though since like the tool isnt anchored or anything so.

did you animate the tool in the animation? If so, does it have a motor6d?

yea, and no it doesnt have a motor6d

did you try to change the AnimationPriority of your animation?

crap forgot that exsisted. It’s fixed though, thanks alot.

1 Like

oh wow, I guess that was the problem all along

Are there any errors in the console?
Can you validate that the animation rig type is the same as the player’s character’s current rig type?

you’re welcome ! can you make it a solution :white_check_mark: to help me?

2 Likes

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