Script doesn't stop

I am having a problem with my script.
The script is supposed to make you sit then have randomly insert a animation that makes player look and then it repeats.
The problem i am having is when you get up from the seat it stops the normal animation but looking animation still randomly comes.
I have tried different ways to stop the script but nothing really works.

If someone can show me what’s wrong i would really appreciate it, i am very new to scripting stuff.
Thank you in advance.

The script:
– Script inside the Seat object
local seat = script.Parent

– Define animation groups (main and look animations for each group)
local animationGroups = {
{ – Group 1: Main + Look Animation
mainAnimationId = “rbxassetid://136018253470936”, – Replace with your main animation ID for group 1
lookAnimationId = “rbxassetid://124579272143729” – Replace with your look animation ID for group 1
},
{ – Group 2: Main + Look Animation
mainAnimationId = “rbxassetid://86463385173746”, – Replace with your main animation ID for group 2
lookAnimationId = “rbxassetid://122811623068351” – Replace with your look animation ID for group 2
},
{ – Group 2: Main + Look Animation
mainAnimationId = “rbxassetid://79961445936757”, – Replace with your main animation ID for group 2
lookAnimationId = “rbxassetid://89032635118664” – Replace with your look animation ID for group 2
}
}

– Function to handle animation selection and playing
local function playAnimations(humanoid)
local animator = humanoid:FindFirstChildOfClass(“Animator”)

if not animator then
	animator = Instance.new("Animator")
	animator.Parent = humanoid
end

-- Randomly choose one of the animation groups on each sit
local chosenGroup = animationGroups[math.random(1, #animationGroups)] -- Randomly select between Group 1 and Group 2
local mainAnimation = Instance.new("Animation")
mainAnimation.AnimationId = chosenGroup.mainAnimationId

local lookAnimation = Instance.new("Animation")
lookAnimation.AnimationId = chosenGroup.lookAnimationId

-- Load animations into tracks
local mainTrack = animator:LoadAnimation(mainAnimation)
local lookTrack = animator:LoadAnimation(lookAnimation)

local stopSignal = false -- Used to stop animations if the player leaves seat

-- Function to stop all animations
local function stopAllAnimations()
	stopSignal = true
	if mainTrack.IsPlaying then mainTrack:Stop() end
	if lookTrack.IsPlaying then lookTrack:Stop() end
	print("All animations stopped.")
end

-- Function to play the main animation and occasionally play the look animation
local function playSequence()
	stopSignal = false
	mainTrack.Looped = true -- Loop the main animation
	mainTrack:Play() -- Start the main animation continuously

	while not stopSignal do
		-- Wait for a random time before deciding to play the look animation
		local waitTime = math.random(10, 20) -- Wait between 10 to 20 seconds for the next check (increased for less frequent checks)
		task.wait(waitTime)

		-- Randomly decide whether to play the look animation (lower chance for less frequency)
		local shouldPlayLook = math.random() < 0.5 -- 10% chance to play the look animation (reduced for less frequency)
		if shouldPlayLook then
			print("Transitioning to look animation.")
			lookTrack:Play() -- Start playing the look animation
			lookTrack.Priority = Enum.AnimationPriority.Action -- Ensure it overrides the main animation temporarily

			-- Explicitly wait for 5 seconds (length of the look animation)
			for count = 0, 50, 1 do
				if stopSignal == false then
					wait(0.1)
				else
					return
				end
			end

			-- Stop the look animation and return to the main animation
			lookTrack:Stop()
			print("Look animation ended, returning to main animation.")
			
			-- Transition back to the main animation
			mainTrack:AdjustWeight(1.0, 0.2) -- Smooth transition back to main animation over 0.2 seconds
		end
	end
end

-- Start the animation sequence
coroutine.wrap(playSequence)()

-- Return the function to stop animations when the player leaves
return stopAllAnimations

end

– Function to handle when a player sits on the seat
local function onSeatOccupied()
local occupant = seat.Occupant
if occupant and occupant:IsA(“Humanoid”) then
local humanoid = occupant

	-- Start playing animations when player sits down
	local stopAnimations = playAnimations(humanoid)

	-- Detect when player leaves the seat and stop animations
	seat:GetPropertyChangedSignal("Occupant"):Connect(function()
		if not seat.Occupant then
			stopAnimations() -- Stop animations if player leaves the seat
		end
	end)
end

end

– Listen for when a player sits on the seat
seat:GetPropertyChangedSignal(“Occupant”):Connect(onSeatOccupied)

2 Likes

You should call the function once again after changing the variable:

-- Start playing animations when player sits down
	local stopAnimations = playAnimations(humanoid)

	-- Detect when player leaves the seat and stop animations
	seat:GetPropertyChangedSignal("Occupant"):Connect(function()
		if not seat.Occupant then
			stopAnimations() -- Stop animations if player leaves the seat
                        playSequence()
		end
	end)
end

Also inside Play sequence function, add a boolean as a parameter inside the function to see if you should play the animation or no:

local function PlaySequence(playanimation) -- true or false
if playanimation then
stopSignal = false
	mainTrack.Looped = true -- Loop the main animation
	mainTrack:Play() -- Start the main animation continuously
end
end

(don’t mind the incorrect spaces im typing this on phone)

3 Likes

try this:

-- Script inside the Seat object

local seat = script.Parent

-- Define animation groups (main and look animations for each group)
local animationGroups = {
    { 
        -- Group 1: Main + Look Animation
        mainAnimationId = "rbxassetid://136018253470936", -- Replace with your main animation ID for group 1
        lookAnimationId = "rbxassetid://124579272143729"  -- Replace with your look animation ID for group 1
    },
    { 
        -- Group 2: Main + Look Animation
        mainAnimationId = "rbxassetid://86463385173746",  -- Replace with your main animation ID for group 2
        lookAnimationId = "rbxassetid://122811623068351"  -- Replace with your look animation ID for group 2
    },
    { 
        -- Group 3: Main + Look Animation
        mainAnimationId = "rbxassetid://79961445936757",  -- Replace with your main animation ID for group 3
        lookAnimationId = "rbxassetid://89032635118664"   -- Replace with your look animation ID for group 3
    }
}

-- Variable to keep track of the current stop function
local currentStopFunction = nil

-- Function to handle animation selection and playing
local function playAnimations(humanoid)
    local animator = humanoid:FindFirstChildOfClass("Animator")
    
    if not animator then
        animator = Instance.new("Animator")
        animator.Parent = humanoid
    end
    
    -- Randomly choose one of the animation groups on each sit
    local chosenGroup = animationGroups[math.random(1, #animationGroups)]
    local mainAnimation = Instance.new("Animation")
    mainAnimation.AnimationId = chosenGroup.mainAnimationId
    
    local lookAnimation = Instance.new("Animation")
    lookAnimation.AnimationId = chosenGroup.lookAnimationId
    
    -- Load animations into tracks
    local mainTrack = animator:LoadAnimation(mainAnimation)
    local lookTrack = animator:LoadAnimation(lookAnimation)
    
    local stopSignal = false -- Used to stop animations if the player leaves seat
    
    -- Function to stop all animations
    local function stopAllAnimations()
        stopSignal = true
        if mainTrack.IsPlaying then 
            mainTrack:Stop() 
        end
        if lookTrack.IsPlaying then 
            lookTrack:Stop() 
        end
        print("All animations stopped.")
    end
    
    -- Start the animation sequence in a new coroutine
    coroutine.wrap(function()
        mainTrack.Looped = true -- Loop the main animation
        mainTrack:Play() -- Start the main animation continuously
        print("Main animation started.")
        
        while not stopSignal do
            -- Wait for a random time before deciding to play the look animation
            local waitTime = math.random(10, 20) -- Wait between 10 to 20 seconds for the next check
            print("Waiting for " .. waitTime .. " seconds before next check.")
            
            for i = 1, waitTime do
                if stopSignal then
                    break
                end
                task.wait(1) -- Wait 1 second at a time to check the stopSignal
            end
            
            if stopSignal then
                break
            end
            
            -- Randomly decide whether to play the look animation (10% chance)
            local shouldPlayLook = math.random() < 0.1
            if shouldPlayLook then
                print("Transitioning to look animation.")
                lookTrack:Play() -- Start playing the look animation
                lookTrack.Priority = Enum.AnimationPriority.Action -- Ensure it overrides the main animation temporarily
                
                -- Wait for the duration of the look animation or until stopped
                local lookDuration = 5 -- Assuming the look animation is 5 seconds long
                for i = 1, lookDuration * 10 do -- Check every 0.1 seconds
                    if stopSignal then
                        break
                    end
                    task.wait(0.1)
                end
                
                if not stopSignal then
                    -- Stop the look animation and return to the main animation
                    lookTrack:Stop()
                    print("Look animation ended, returning to main animation.")
                    
                    -- Smooth transition back to main animation
                    mainTrack:AdjustWeight(1.0, 0.2) -- Adjust as needed
                end
            end
        end
        
        -- Ensure the main animation is stopped when exiting the loop
        if mainTrack.IsPlaying then 
            mainTrack:Stop() 
        end
        print("Animation sequence ended.")
    end)()
    
    return stopAllAnimations
end

-- Function to handle when a player sits on the seat
local function onOccupantChanged()
    local occupant = seat.Occupant
    if occupant and occupant:IsA("Humanoid") then
        -- If there's an existing animation sequence, stop it first
        if currentStopFunction then
            currentStopFunction()
        end
        
        local humanoid = occupant
        -- Start playing animations when player sits down
        currentStopFunction = playAnimations(humanoid)
    else
        -- If there's an existing animation sequence, stop it
        if currentStopFunction then
            currentStopFunction()
            currentStopFunction = nil
        end
    end
end

-- Connect the function to the Occupant property change
seat:GetPropertyChangedSignal("Occupant"):Connect(onOccupantChanged)

2 Likes