Play Animation from Player Animation List/Script when touching a part

I’m trying to make characters play specific animations when sitting on certain parts that are only available inside the player’s morph. Like for example, a part meant for a sitting on the floor animation will play a different sit animation from a part meant for a sitting on a chair animation.
image
image

Something else I’ve tried is making a localscript inside the player that checks for a folder with a specific name in the workspace so every time a player sits on a part in that folder the designated animation plays, but that doesn’t work.
image
image
I can guarantee I’m missing something but I’m not exactly the best scripter so I can’t figure anything out.

I have searched all over the DevForum for some sort of answer but I’ve gotten next to no answers. I’m pretty sure what I’m trying to do also isn’t something that someone normally tries without already knowing how to do it.

Here’s the full script from the localscript within the player:
—Script—
local floorsit = script:WaitForChild(“FloorSit”)
local benchsit = script:WaitForChild(“BenchSit”)
local humanoid = script.Parent:FindFirstChild(“Humanoid”)
local animTrack1 = humanoid.Animator:LoadAnimation(floorsit)
local animTrack2 = humanoid.Animator:LoadAnimation(benchsit)

local function FloorSit()
for _, possiblePart in pairs(workspace.FloorSeats:GetChildren()) do
humanoid:GetPropertyChangedSignal(“Sit”):Connect(function()
if possiblePart:IsA(“BasePart”) then
if humanoid.Sit == true then
animTrack1:Play()
else
animTrack1:Stop()
end
end
end)
end
end

If anyone is able/willing to help I’d be most grateful, thank you!

5 Likes

This code is structured strangely, here is a better way to get to your desired result.

  • Get the PropertyChangedSignal of the Humanoid’s SeatPart
  • When it changes, check if the SeatPart is not equal to nil
  • If it isn’t nil and its parent is equal to FloorSeats then, play the animation
  • If it is equal to nil, then stop the animation.

Additionally, if this doesn’t fix the issue, you can try changing the AnimationPrority to be over that of the Humanoid’s sit animation.

Hope this helps.

1 Like

I think it would be able to help if I knew exactly how to structure this sort of thing in a script. It’s more advanced then what I’m used to doing so I’m not exactly sure what to do here. If you’re able to help with a script layout then I’d be most appreciative.

1 Like

So, inside of your floorsit function, you would do Humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function())
to get an event whenever the seatpart changes.

Then, you check if the seatpart exists or not

if Humanoid.SeatPart then

If it exists, then check if it’s parent is workspace.FloorSeats

If Humanoid.SeatPart.Parent == workspace.FloorSeats then

If that passes, then play your animation.

now, if the Humanoid.SeatPart == nil then, you would stop the animation. Hope that helped.

Thanks for the help so far, it still doesn’t work as of now so I may have typed the script part wrong. Any advice?

local function FloorSit()
humanoid:GetPropertyChangedSignal(“SeatPart”):Connect(function()
if humanoid.SeatPart then
if humanoid.SeatPart.Parent == workspace.FloorSeats then
animTrack1:Play()
if humanoid.SeatPart.Parent == nil then
animTrack1:Stop()
end
end
end
end)
end

Also my humanoid uses a lowercase capital so no that wouldn’t be the issue. Thanks!

Do you ever run the FloorSit() function? also, Instead of explicitly checking for it being nil, you could just use else.

I don’t think I do, though to be fair I’m not very used to using functions so I’m not sure.

Can you provide the full script?

Sure, here it is.

local floorsit = script:WaitForChild(“FloorSit”)
local benchsit = script:WaitForChild(“BenchSit”)
local humanoid = script.Parent:FindFirstChild(“Humanoid”)
local animTrack1 = humanoid.Animator:LoadAnimation(floorsit)
local animTrack2 = humanoid.Animator:LoadAnimation(benchsit)

local function FloorSit()
humanoid:GetPropertyChangedSignal(“SeatPart”):Connect(function()
if humanoid.SeatPart then
if humanoid.SeatPart.Parent == workspace.FloorSeats then
animTrack1:Play()
else
animTrack1:Stop()
end
end
end)
end

All the way at the end of the script, just write FloorSit() and it should work.

IT WORKS! Only issue is that sometimes between animations the character seems to try playing the sit animation again, at least until you sit on a different part. It’s not a big deal though. If you have any suggestions for a way to fix that though then thank you for time and patience with me!

Under the character, theres a script called “Animate” this script can be disabled to remove all default animations, or the Ids underneath it can be modified to make custom animations

It’s a custom animation set, it just tries playing the animation from that seat part, since it fixes itself when sitting on a different part, which as of now doesn’t have an animation assigned to it meaning there is no animation to play.

Can you send a video of the behavior? I don’t really understand what you are saying

Hopefully the video shows it well. You can see before sitting there’s no issues, but after sitting, the character tries to play a sit animation between doing animations. So I don’t think the animation stop part of the script is exactly working.

1 Like

The animation seems fine, it’s just playing the Jump animation.

No see, between animations after sitting, it tries to play the sit animation before playing the correct animation, meaning the sit animation would be trying to play in the background. Which means the animtrack1:Stop() isn’t working properly.

Oh, I see, its playing the sit animation midair. This is because you need to put the “else” after 1 end

local floorsit = script:WaitForChild(“FloorSit”)
local benchsit = script:WaitForChild(“BenchSit”)
local humanoid = script.Parent:FindFirstChild(“Humanoid”)
local animTrack1 = humanoid.Animator:LoadAnimation(floorsit)
local animTrack2 = humanoid.Animator:LoadAnimation(benchsit)

local function FloorSit()
humanoid:GetPropertyChangedSignal(“SeatPart”):Connect(function()
if humanoid.SeatPart then
if humanoid.SeatPart.Parent == workspace.FloorSeats then
animTrack1:Play()
end
else
animTrack1:Stop()
end
end)
end
1 Like

And that’s everything fixed now. Thank you so much for your help! And also thanks for having the patience to put up with this.

2 Likes