Animation onTouch?

Okay, so I tried to make an animation play whenever an enemy NPC was touched. Unfortunately I hit a road bump and yet again have no idea what to do.
I don’t know how to make an animation play onTouch!

I still looked everywhere, tutorials, on the Dev Forum, and the API on the developer website. But I couldn’t find anything I could use.

I tried to use this script

local lhand = script.Parent.LeftHand
local rhand = script.Parent.RightHand

local function onTouch(otherPart)
local hum = otherPart.Parent:FindFirstChild("Humanoid")
 if hum then
PlayAnimation:"{rbxassetid://5053427576}"

lhand.Touched:Connect(onTouch)
rhand.Touched:Connect(onTouch)

And it obviously didn’t work.
Is there anything I should add / remove to make this script work?

1 Like

From what I know .Touched doesn’t have any parameters so “otherPart” would be nil. I would suggest doing this:

lhand.Touched:Connect(function()
     onTouch(lhand)
end)
rhand.Touched:Connect(function()
     onTouch(rhand)
end)

Edit: I also just read the code a bit more it seems you forgot an “end” to close the “if hum then” if statement.

Thanks I’ll test it out to see if it works.

local lhand = script.Parent.LeftHand
local rhand = script.Parent.RightHand
local Anim = Instance.new("Animation",game.ServerStorage)
Anim.AnimationId = "rbxassetid://5053427576"

lhand.Touched:Connect(function(hit)
	local Hum = hit.Parent:FindFirstChild("Humanoid")
	if Hum then
		local Animation = Hum:LoadAnimation(Anim)
		Animation:Play()
	end
end)


rhand.Touched:Connect(function(hit)
	local Hum = hit.Parent:FindFirstChild("Humanoid")
	if Hum then
		local Animation = Hum:LoadAnimation(Anim)
		Animation:Play()
	end
end)
1 Like

Touched does have an paramater, its paramater is the part that touched it, please don’t spread miss information.

I said “From what I know” I wasn’t completely sure and don’t have tons of time to go check the Developer Hub right now.

https://www.google.com/search?q=.Touched+developer.roblox.com&oq=.Touched+developer.roblox.com&aqs=chrome..69i57.8182j0j7&sourceid=chrome&ie=UTF-8

“Tons of time”

Hey the script didn’t work right. Do I have to put the script inside something? I tried to put it in humanoid but it didn’t work right.

Also I meant the NPC did the animation. Sorry if it wasn’t specified.

I think you should have let it stay at the same place it were.

Wait, i can’t understand, it did the animation but didn’t work???

No the animation didn’t work. Maybe the script had something to do with it.
When I tested the game with the script in the NPC the NPC did not do the animation.

Then put this script in ServerScriptService:

local lhand = game.Workspace -- Put here where the LeftHand is at.
local rhand = game.Workspace -- Put here where the RightHand is at.
local Anim = Instance.new("Animation",game.Workspace)
Anim.AnimationId = "rbxassetid://5053427576"

lhand.Touched:Connect(function(hit)
	local Hum = hit.Parent:FindFirstChild("Humanoid")
	if Hum then
		local Animation = Hum:LoadAnimation(Anim)
		Animation:Play()
	end
end)


rhand.Touched:Connect(function(hit)
	local Hum = hit.Parent:FindFirstChild("Humanoid")
	if Hum then
		local Animation = Hum:LoadAnimation(Anim)
		Animation:Play()
	end
end)

Ok, i edited an part of the script, use the edited one instead. The script above. ^^^

3 Likes

I’ll test it out to see if it works.
I’ll edit my comment once I tested it.
Edit: I’m kind of confused but I’ll try to find a way. Thanks.

1 Like