Animation not working when touching a part

Hey, I’m having trouble identifying what’s wrong with this. Basically when you touch the part, It’ll change your animation but for some reason it won’t work here.

Here’s the script:

function onTouched(hit)
    local human = hit.Parent:findFirstChild("Humanoid")
    if human then
        local player = game.Players.LocalPlayer
        local character = player.CharacterAdded:wait()
        character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://6007672361"
    end
end
script.Parent.Touched:Connect(onTouched)
2 Likes

You have to own the animation.

I do own the animation, just won’t work.

I would specify the animation like putting it in a animation in workspace or something specify it in a variable. Call it by Animation:Play()

As so?:

local animation =  character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://6007672361"
function onTouched(hit)
    local human = hit.Parent:findFirstChild("Humanoid")
    if human then
        local player = game.Players.LocalPlayer
        local character = player.CharacterAdded:wait()
        animation:Play()
    end
end
script.Parent.Touched:Connect(onTouched)

I mean like making a animation object in workspace and putting the ID as a value:
image
place to put animation ID:
image

I tried, it didn’t work and I used the current script that I originally posted.

You have to use humanoid.Animator as far as I know
Animate and animation controller are all deprecated

local humanoid = rig:FindFirstChild(“Humanoid”)
local animator = humanoid:WaitForChild(“Animator”)

You can have an animation inside this script in which you can change the animation ID

local animation = script:WaitForChild(“Animation”)
local loadedAnimation = animator:LoadAnimation(animation)
loadedAnimation:Play()

So…
Here is my implementation of this code in your provided script:

function onTouched(hit)
local human = hit.Parent:findFirstChild("Humanoid")
if human then
     local animator = human:WaitForChild(“Animator”)
     local animation = Instance.new(“Animation”)
     animation.AnimationId = “http://www.roblox.com/asset/?id=507771019”
local loadedAnimation = animator:LoadAnimation(animation)
loadedAnimation:Play()
end
end
script.Parent.Touched:Connect(onTouched)

API References:

Edit: I did this on mobile so the quotations are messed up

1 Like

1 Like

The walk animation Id in the player Isn’t changing.

What type of animation is it? If you didn’t set it to an Action type, it’ll not show up due to priorities

local touchPart = script.Parent -- your touch part
local animationId = 6007672361 -- your custom animation id
touchPart.Touched:Connect(function(hit)
	local char = hit.Parent
	if char then
		local animate = char:FindFirstChild("Animate")
		if animate then
			animate.walk.WalkAnim.AnimationId = "rbxassetid://".. animationId
			animate.run.RunAnim.AnimationId = "rbxassetid://".. animationId
		end
	end
end)

Once you touch the part, it should change your walk animation to custom one, and also make sure that your animation has proper priority set.

1 Like

Is this in a local or normal script?

I think its not working due to you are using deprecated methods to get the character, and its a LocalScript, so theres no point to check what humanoid has touched the part, it will always be your humanoid.

local Player = game:GetService(“Players”).LocalPlayer or game:GetService(“Players”).PlayerAdded:Wait()
local Character = Player.Character or Player.CharacterAdded:Wait() or workspace:FindFirstChild(Player.Name)
local Humanoid = Character:WaitForChild(“Humanoid”)

function onTouched(hit)
	if (Humanoid and Humanoid.Health > 0) then
		local Animate = Character:FindFirstChild("Animate")
		if Animate then
			Animate:FindFirstChild("walk").WalkAnim.AnimationId = "rbxassetid://6007672361"
		else
			warn("Animate script wasn't found on " .. Character:GetFullName())
		end
	end
end

script.Parent.Touched:Connect(onTouched)

Try that, i just think you should use a Server Script instead a Local one.

2 Likes