Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make my animation script work in a server?

Asked by 6 years ago

Script works in studio, not in-game

The script animates a person who is sitting on a seat. It only works when I play-test through studio but not through the actual ROBLOX website. The code is in a Script not a LocalScript

I am not very good at scripting so I would really appreciate if you could tell me where to fix my code.

local seat = script.Parent

function SeatIsSatIn(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local character = hit.Parent
        local humanoid = character.Humanoid
        wait(0.5) -- Wait for character to sit
        if humanoid:GetState() == Enum.HumanoidStateType.Seated then -- Making sure the humanoid is sitting
            local animation = Instance.new("Animation")
            animation.AnimationId = "http://www.roblox.com/asset/?id=1336275596" -- AnimationID here
            local  track = humanoid:LoadAnimation(animation)
            track:Play()
            local function StateChanged(oldState, newState)
                if newState ~= Enum.HumanoidStateType.Seated then
                    track:Stop()
                end
            end

            humanoid.StateChanged:connect(StateChanged)
        end
    end
end

seat.Touched:connect(SeatIsSatIn)

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

You could try something like this instead:

local seat = script.Parent
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    if not seat.Occupant then return end
    local anim = Instance.new("Animation")
    anim.AnimationId = "http://www.roblox.com/asset/?id=1336275596"
    local track = seat.Occupant:LoadAnimation(anim)
    track.Priority = Enum.AnimationPriority.Action
    track:Play()
    seat:GetPropertyChangedSignal("Occupant"):Wait()
    track:Stop()
end)
0
Thank you so much! At first it wasn't working but I added a delay to allow the player to sit down. I really appreciate the answer, thank you! JasonSasuke 0 — 6y
Ad

Answer this question