Script randomly stopping mid-run without error

Hi!
I’m currently trying to animate the doors of the train I’m making, and during the scripting process I got following issue:

The doors open and then after 15 seconds they close, just like it’s supposed to be. However, after trying it out the second time, the doors won’t close. There is nothing in the output. The animations for both opening and closing are included in one file, that’s why I stop the animation with the :AdjustSpeed function.

What I tried to fix the issue:

  • Changing values to higher and lower numbers
  • Turning off Filtering Enabled
  • Turning off Team Create
  • Inserting ‘print’ functions in the script, it seems to stop after the ‘OpenDoors’ function is done.

The script is attatched here.

local ClickDetector = script.Parent.Button.ClickDetector
local Humanoid = script.Parent.Humanoid
local Animation = Humanoid:LoadAnimation(script.Parent.Animation)

function OpenDoors()
	ClickDetector.MaxActivationDistance = 0
	Animation:Play()
	wait(2)
	Animation:AdjustSpeed(0)
end

function CloseDoors()
	Animation:AdjustSpeed(1)
	wait(2)
	ClickDetector.MaxActivationDistance = 16
end

ClickDetector.MouseClick:Connect(OpenDoors)
wait(15)
CloseDoors()

Thanks for reading!

The problem here is that you connect the function OpenDoors to the click, then you wait 15 seconds and close the door whether it was open or not. But after that, you don’t call CloseDoors again anywhere. To fix that you would need to make everything happen in a single function or create another function bound to the click that calls both open and close

local ClickDetector = script.Parent.Button.ClickDetector
local Humanoid = script.Parent.Humanoid
local Animation = Humanoid:LoadAnimation(script.Parent.Animation)

function OpenDoors()
	ClickDetector.MaxActivationDistance = 0
	Animation:Play()
	wait(2)
	Animation:AdjustSpeed(0)
end

function CloseDoors()
	Animation:AdjustSpeed(1)
	wait(2)
	ClickDetector.MaxActivationDistance = 16
end

ClickDetector.MouseClick:Connect(function() --This function both opens, then closes the doors
    OpenDoors()
    wait(15)
    CloseDoors()
end)
2 Likes

Like mentioned above the code isn’t calling the wait and the close more than the initial time the script runs, but the open is connected to the click… so you need to connect the wait and the close as well. I would also add a denounce instead of changing the click distance, personally… sorry if my tabs are messed up I’m on my phone.

1 Like
local ClickDetector = script.Parent.Button.ClickDetector
local Humanoid = script.Parent.Humanoid
local Animation = Humanoid:LoadAnimation(script.Parent.Animation)

function CloseDoors()
	Animation:AdjustSpeed(1)
        wait(2)
        debounce = false
end

function OpenDoors()
        if not debounce then
             debounce = true
	     Animation:Play()
	     wait(2)
	     Animation:AdjustSpeed(0)
             wait(15) 
             CloseDoors()
        end
end

ClickDetector.MouseClick:Connect(OpenDoors)
ClickDetector.MouseClick:Connect(CloseDoors) -- you forgot this
1 Like

No… it’s called in the open function how i wrote it, that would also require a second click to close it…

1 Like

How… You did it for open doors but not close doors?

Now that was just a stupid error by me, thank you!

The second click was intentionally left out, because the doors only close automatically after the said 15 seconds or manually by the driver.

If you’re using this code for anything more than just personal testing purposes, I would still suggest using the debounce over the changing of the activation distance at least.

Not that it really matters in this case (most likely), I would prefer to write it like this where you don’t add the third function which calls both functions, just connect the open function which then calls the close on its own. Anyways, have fun!

local ClickDetector = script.Parent.Button.ClickDetector
local Humanoid = script.Parent.Humanoid
local Animation = Humanoid:LoadAnimation(script.Parent.Animation)
local debounce = false

function CloseDoors()
	Animation:AdjustSpeed(1)
	wait(2)
	debounce = false
end

function OpenDoors()
	if not debounce then
		debounce = true
		Animation:Play()
		wait(2)
		Animation:AdjustSpeed(0)
		wait(15) 
		CloseDoors()
	end
end

ClickDetector.MouseClick:Connect(OpenDoors)
1 Like