Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I made a custom sitting animation seat but I have a problem. Help?

Okay, So basically I made a chair model, with a seat, inside the seat is a script, and inside the script is a animation.

So the script is

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    hit.Parent.Humanoid.Seated:Connect(function()
        local animationtoplay = hit.Parent.Humanoid:LoadAnimation(script.Animation)
        animationtoplay:Play()
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
        while wait() do
            if Player.Character.Humanoid.SeatPart == nil then
                animationtoplay:Stop()
		print("made by yealetfng please dont steal")
            end
            end
        end)
    end
end)

it all works yet whenever you go to a different seat it wont play that animation in that seat it will play the animation of the seat you first sat in.

if anyone can help me with this would be greatly appreciated!

2 comments

0

does the other seat have a different animation script?

0

yes each seat has a different animation ID in it

1 answer

✓ Accepted answer

The problem here is that once a seat is touched the Humanoid.Seated connection in it persists, it has not been disconnected and will fire for other seats as well. You can fix this by disconnecting the event right after its first run:


script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    local connection 
    connection = hit.Parent.Humanoid.Seated:Connect(function()
        local animationtoplay = hit.Parent.Humanoid:LoadAnimation(script.Animation)
        animationtoplay:Play()
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
        while wait() do
            if Player.Character.Humanoid.SeatPart == nil then
                animationtoplay:Stop()
        print("made by yealetfng please dont steal")
                connection:Disconnect()
            end
            end
        end)
    end
end)

Another way to tackle this is to simply adjust the animation according to the seat sat on from within the connection, in a manner that you don’t have to place individual scripts, all you have to do is place the animation directly under the respective seats:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid", 3)
        humanoid.Seated:Connect(function(sat, seat)
            if sat then
                 local animation = humanoid:LoadAnimation(seat.Animation)
                 animation:Play()
            end
        end)
    end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.