How to make start animate when touching an object

Help pls. I’m need this for my tycoon

puedes ser mas claro de lo que quieres hacer como cuando dejas de tocar el objeto deja de funcionar la animación

Quiero hacer cuando el jugador aparece y la animación se realiza solo cuando el jugador está parado sobre él, y cuando el jugador no está parado sobre él, la animación se detiene. (uso el traductor de Google, no sé español)

Example.


I want to do when the player stands on the button, it starts the animation, and when the player leave, the animation, it goes back to its position

try with this script

function onTouched(hit)
	local human = hit.Parent:findFirstChild("Humanoid")
	if human then
	local anim = human:LoadAnimation(script.Parent.Animation)
	anim:Play()
	end
end
script.Parent.Touched:connect(onTouched)
1 Like

do I need to create a new script for this? or?

si dentro de la part que quieres animar

timeline for these posts wack; I’m strictly an English speaker so sorry for anything misunderstood.

Referencing the script @sergizan37 posted; adding some quality of life practices

local Touched,TouchEnded = nil,nil;
local onTouched,onTouchEnded;

local This = script.Parent;
local OurAnimation = This.Animation;

onTouchEnded = function()
  pcall(function() Touched:Disconnect() end) -- ensure there's no active event
  Touched = This.Touched:Connect(onTouched) -- connect touched, set variable
  pcall(function() TouchEnded:Disconnect() end) -- finally, disconnect this event
end

onTouched = function(ObjectHit)
	local Humanoid = ObjectHit.Parent:FindFirstChildOfClass("Humanoid")
	if Humanoid then
	  local AnimationTrack = Humanoid:LoadAnimation(OurAnimation)
      AnimationTrack.Stopped:Connect(function() -- No need to ref this connection
        AnimationTrack:Destroy(); -- this is good practice. It will disconnect..
        -- ..all events (including this one, that's why we needn't reference it)
      end)
	  AnimationTrack:Play()
     TouchEnded = This.TouchEnded:Connect(onTouchEnded)
     pcall(function()
       Touched:Disconnect() -- this prevents Touched from spamming. This is a..
       -- ..well known issue with touched events.
      end) -- and we wrap it in a pcall to ensure it doesn't break execution if...
      -- ..it errors. Although, it's in an event connection so it shouldn't.
	end
end
Touched = This.Touched:connect(onTouched)

Touched events are unbiased towards what is “touching” them. In this case this means it will fire all connections to Touched for a single part for every tick of Stepped (Physics calculation stage in the Engine pipeline.)
Therefore, it is recommended (and good practice,) to have recursive connections for Touched and TouchEnded.


The following is the pipeline of the ROBLOX .Touched experience

Touched fires → evaluates HitResult based on user code → connects TouchEnded → disconnects SelfHitResult object stops touching target → TouchEnded fires → might evaluate HitResult after it stops touching → double checks – in pcall; for a Touched connection → disconnect Touched if it exists → reconnect Touched → disconnects Self

And the cherry on top is that, if you’re fluent in lua, you deciphered that and found that it…

Cycles right back to Step 1

Hope this post helps.

EDIT: I’m sorry if you’ve got any questions, but I’m gonna go to sleep; rather tired and got an awful shift right as classes end today.
Sergizan should be able to assist you if need-be.

No soy un buen programador asi que le habrias ayudado mejor tu.

1 Like

Google translate got this one right; pog.
Sorry for my late replies, lot of work to do around the house.
I’ll be wrapping up a draft really quickly before I reply again.