I’m making a train door animation, and when i opening/closing doors fast after open animation they closing and when close animation they opening and this is because function is not ended. How to stop it? Again, THIS IS NOT RBX CONNECTION!!
local module = {}
local tween = game:GetService("TweenService")
local tween1O
local tween2O
local tween3O
local tween1C
local tween2C
local tween3C
local doorstatus = "Closed"
function FindFirstDescendant(name)
for i,k in pairs(script.Parent.Parent:GetDescendants()) do
if k.Name == name then
return k
end
end
end
function playsound(name)
for i,s in pairs(script.Parent.Parent:GetDescendants()) do
coroutine.wrap(function()
if s.Name == name then
if s:IsA("Sound") then
s:Play()
end
end
end)()
end
end
function module.Close(SIDE, doorspeed)
module.Open:Disconnect() -- ?
local find = FindFirstDescendant(SIDE.."Door")
if doorspeed <= 0 then
doorspeed = 1
end
print("tween open stoped")
doorstatus = "Closing"
for i,k in pairs(find:GetChildren()) do
coroutine.wrap(function()
if k:IsA("PrismaticConstraint") then
k.Speed = 1
playsound("Dclose")
tween1C = tween:Create(k, TweenInfo.new(doorspeed-1, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {TargetPosition = 0})
tween1C:Play()
tween2C = tween:Create(k, TweenInfo.new(1, Enum.EasingStyle.Exponential), {Speed = doorspeed * 2})
tween2C:Play()
wait((doorspeed-1) / 2.66666666667)
tween3C = tween:Create(k, TweenInfo.new(0.1 * doorspeed, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {TargetPosition = 0})
tween3C:Play()
tween3C.Completed:Wait()
doorstatus = "Closed"
end
end)()
end
end
function module.Open(SIDE, doorspeed)
module.Close:Disconnect() -- ?
local find = FindFirstDescendant(SIDE.."Door")
if doorspeed <= 0 then
doorspeed = 1
end
doorstatus = "Opening"
print("tween close stoped")
for i,k in pairs(find:GetChildren()) do
coroutine.wrap(function()
if k:IsA("PrismaticConstraint") then
k.Speed = 1
playsound("Dopen")
tween1O = tween:Create(k, TweenInfo.new(doorspeed, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {TargetPosition = 2.74})
tween1O:Play()
tween2O = tween:Create(k, TweenInfo.new(1, Enum.EasingStyle.Exponential), {Speed = doorspeed * 2})
tween2O:Play()
wait(doorspeed / 2.66666666667)
tween3O = tween:Create(k, TweenInfo.new(0.1 * doorspeed, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {TargetPosition = 2.74})
tween3O:Play()
tween3O.Completed:Wait()
doorstatus = "Opened"
end
end)()
end
end
return module
local thread = task.spawn(FUNCTIONHERE)
task.cancel(thread) -- cancel whenever you want
Why i have error?
local module = {}
local tween = game:GetService("TweenService")
local tween1O
local tween2O
local tween3O
local tween1C
local tween2C
local tween3C
local doorstatus = "Closed"
function FindFirstDescendant(name)
for i,k in pairs(script.Parent.Parent:GetDescendants()) do
if k.Name == name then
return k
end
end
end
function playsound(name)
for i,s in pairs(script.Parent.Parent:GetDescendants()) do
coroutine.wrap(function()
if s.Name == name then
if s:IsA("Sound") then
s:Play()
end
end
end)()
end
end
function module.Close(SIDE, doorspeed)
local threadO = task.spawn(module.Open)
task.cancel(threadO) -- stops open animation
local find = FindFirstDescendant(SIDE.."Door")
if doorspeed <= 0 then
doorspeed = 1
end
print("tween open stoped")
doorstatus = "Closing"
for i,k in pairs(find:GetChildren()) do
coroutine.wrap(function()
if k:IsA("PrismaticConstraint") then
k.Speed = 1
playsound("Dclose")
tween1C = tween:Create(k, TweenInfo.new(doorspeed-1, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {TargetPosition = 0})
tween1C:Play()
tween2C = tween:Create(k, TweenInfo.new(1, Enum.EasingStyle.Exponential), {Speed = doorspeed * 2})
tween2C:Play()
wait((doorspeed-1) / 2.66666666667)
tween3C = tween:Create(k, TweenInfo.new(0.1 * doorspeed, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {TargetPosition = 0})
tween3C:Play()
tween3C.Completed:Wait()
doorstatus = "Closed"
end
end)()
end
end
function module.Open(SIDE, doorspeed)
local threadC = task.spawn(module.Close)
task.cancel(threadC) -- stops close animation
local find = FindFirstDescendant(SIDE.."Door")
if doorspeed <= 0 then
doorspeed = 1
end
doorstatus = "Opening"
print("tween close stoped")
for i,k in pairs(find:GetChildren()) do
coroutine.wrap(function()
if k:IsA("PrismaticConstraint") then
k.Speed = 1
playsound("Dopen")
tween1O = tween:Create(k, TweenInfo.new(doorspeed, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {TargetPosition = 2.74})
tween1O:Play()
tween2O = tween:Create(k, TweenInfo.new(1, Enum.EasingStyle.Exponential), {Speed = doorspeed * 2})
tween2O:Play()
wait(doorspeed / 2.66666666667)
tween3O = tween:Create(k, TweenInfo.new(0.1 * doorspeed, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {TargetPosition = 2.74})
tween3O:Play()
tween3O.Completed:Wait()
doorstatus = "Opened"
end
end)()
end
end
return module
Because its recursive.
When you open a thread it will run the function and since you open another thread in that function, it opens another thread and causing the script to error and etc.
The person calling the function has to do it manually.
There is no real fix, the person calling the function has to the task.spawn thing themselves.
This how it would like how to make it stop:
local module = require(pathtomodule)
local newThread = module.Open(idk, idk)
task.cancel(newThread) -- immediately closes the function
Alternatively coroutines are also an option but same thing
Is there an alternative to stop tween?