I want the player to do a certain animation when they sit down on the seat.
when they sit and jump off the seat it keeps doing the same animation and won’t stop.
I tried different scripts to fix this and they won’t work
local Sit = script.Parent
local SitAnim = script:WaitForChild("Sit")
Sit:GetPropertyChangedSignal("Occupant"):Connect(function()
local Hum = Sit.Occupant
if Hum then
local loadedAnim = Hum:LoadAnimation(SitAnim)
local Connection
loadedAnim:Play()
Connection = Hum:GetPropertyChangedSignal("Sit"):Connect(function()
if Hum == false then
loadedAnim:Stop()
end
end)
end
end)
2 Likes
Instead of detecting when the humanoid stops sitting to stop the animation, try detecting when the seat’s Occupant
is changed to nil
.
1 Like
Unfortunately, I was unable to fix the script you provided. However, the following script should serve the same purpose (the following should be in a LocalScript in StarterPlayerScripts, and the animation should be placed in StarterCharacterScripts):
local player = game.Players.LocalPlayer
local character
if player.Character then
character = player.Character
else
player.CharacterAdded:Wait()
character = player.Character
end
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
local loadedAnimation
if animator then
loadedAnimation = animator:LoadAnimation(character:WaitForChild("Sit"))
else
animator = Instance.new("Animator", humanoid)
loadedAnimation = animator:LoadAnimation(character:WaitForChild("Sit"))
end
humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
if humanoid.Sit == true then
loadedAnimation:Play()
else
loadedAnimation:Stop()
end
end)
4 Likes
how can i make a laying animation on a bed/seat on bed module with this script in play? or do i change something? Also, thanks for figuring this out am very new.
You can use something similar, but it is somewhat more complex. Here are the modified scripts:
LocalScript: (place this in StarterPlayerScripts)
local player = game.Players.LocalPlayer
local character
if player.Character then
character = player.Character
else
player.CharacterAdded:Wait()
character = player.Character
end
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
local loadedAnimation
local loadedAnimation2
if animator == nil then
animator = Instance.new("Animator", humanoid)
end
loadedAnimation = animator:LoadAnimation(character:WaitForChild("Sit"))
loadedAnimation2 = animator:LoadAnimation(character:WaitForChild("[Animation name]")) --Enter the name of the animation you want to load here
--[[Because there is no property for determining whether a humanoid is laying down, I recommend you add
an attribute to the character's humanoid (attributes can be created or updated using ":SetAttribute()").
It is usually best to do this from the server for multiple reasons, so the attribute will be added in a
separate server - side script.]]
humanoid:GetAttributeChangedSignal("Laying"):Connect(function() --This function detects when the attribute's value has changed.
if humanoid:GetAttribute("Laying") == true then
loadedAnimation2:Play()
else
loadedAnimation2:Stop()
end
end)
humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
if humanoid.Sit == true then
loadedAnimation:Play()
else
loadedAnimation:Stop()
end
end)
Server (normal) Script: (place this in StarterCharacterScripts along with the animations)
local humanoid = script.Parent:WaitForChild("Humanoid")
humanoid:SetAttribute("Laying", false) --This attribute will have to be set manually (from a script) whenever you want the animation to play. You can use ":SetAttribute()" for updating attributes as well as creating new ones.