Hi all,
I animated doors for my train and scripted it to a button, but the animation only plays after the second click. Does anyone know why?
LocalScript:
local btn = script.Parent
local plr = game.Players.LocalPlayer
local event = game:GetService("ReplicatedStorage").doors
btn.MouseButton1Click:Connect(function()
local train = plr.Character.Humanoid.SeatPart.Parent
event:FireServer(train, "left")
btn.Visible = false
wait(5)
btn.Visible = true
end)
Script:
local event = game:GetService("ReplicatedStorage").doors
local doorsleft = script.Left
local doorsright = script.Right
local bool = true
event.OnServerEvent:Connect(function(p, train, side) -- the disabled code is for testing
for i, car in pairs(train:GetChildren()) do
if bool == true then
if side == "left" then
if car.Name == "Carriage1" then
local model = car.Model
local hum = model.Humanoid
local animator = hum:WaitForChild("Animator")
task.wait(0.5)
local leftTrack = animator:LoadAnimation(doorsleft)
leftTrack:Play()
leftTrack:AdjustSpeed(1)
task.wait(leftTrack.Length-0.05)
leftTrack:AdjustSpeed(0)
end
elseif side == "right" then
if car.Name == "Carriage1" then
local model = car.Model
local hum = model.Humanoid
local animator = hum:WaitForChild("Animator")
task.wait(0.5)
local leftTrack = animator:LoadAnimation(doorsright)
leftTrack:Play()
leftTrack:AdjustSpeed(1)
task.wait(leftTrack.Length-0.05)
leftTrack:AdjustSpeed(0)
end
end
elseif bool == false then
if side == "left" then
if car.Name == "Carriage1" then
local model = car.Model
local hum = model.Humanoid
local animator = hum:WaitForChild("Animator")
task.wait(0.5)
local leftTrack = animator:LoadAnimation(doorsleft)
leftTrack:Play(0.100000001, 1, -1)
task.wait(leftTrack.Length-0.01)
leftTrack:AdjustSpeed(0)
end
elseif side == "right" then
if car.Name == "Carriage1" then
local model = car.Model
local hum = model.Humanoid
local animator = hum:WaitForChild("Animator")
task.wait(0.5)
local leftTrack = animator:LoadAnimation(doorsright)
leftTrack:Play(0.100000001, 1, -1)
task.wait(leftTrack.Length-0.01)
leftTrack:AdjustSpeed(0)
end
end
end
end
bool = not bool
end)
Any help is appreciated, this has been annoying. Thanks!