Hello! So, I'm currently developing a system for my trains to move with, but I've ran into a sort of dead end.
Basically, whenever I call the :TweenCarriages() function, it just throws an error:
attempt to call nil value.
I know it's not a problem with the script that's calling.
local TweenService = game:GetService("TweenService")
local TrainService = {}
TrainService.__index = TrainService
function TrainService.new(trainModel)
assert(typeof(trainModel) == 'Instance')
local self = setmetatable({},TrainService)
self.TrainModel = trainModel
self.CurrentNode = 1
self.Carriages = {}
self.Speed = 0
return self
end
function TrainService:CreateNewCarriage(carriagePart)
table.insert(self.Carriages, carriagePart)
--print(typeof(self.Carriages[1]))
end
function TrainService:CalculateTime(distance, speed)
local Time = distance / speed
return Time
end
function TrainService:TweenCarriage(tweenTime, nextNode, lastNode, carriageNumber)
if not assert(typeof(nextNode) == "Instance" and nextNode:IsA("BasePart"), "nextNode must be a BasePart") then return end
if not assert(typeof(lastNode) == "Instance" and lastNode:IsA("BasePart"), "lastNode must be a BasePart") then return end
if not assert(tonumber(tweenTime), "tweenTime must be a Number") then return end
local info = TweenInfo.new(
tweenTime,
Enum.EasingStyle.Linear,
Enum.EasingDirection.InOut,
0, -- RepeatCount (-1 = Infinite)
false, -- Reverse tween after finishing
0 -- DelayTime
)
local rot = nextNode.Rotation
local rotY = nil
local dotProduct = self.Carriages[carriageNumber].CFrame.LookVector:Dot(nextNode.CFrame.LookVector)
if (dotProduct > 0.75) then
rotY = nextNode.Rotation.Y
-- Do something with dotY
elseif (dotProduct < -0.75) then
rotY = nextNode.Rotation.Y+180
-- Do something else with rotY
else
rotY = nextNode.Rotation.Y
warn("An issue has occurred.")
end
local position = {CFrame = CFrame.new(nextNode.Position)*CFrame.Angles(math.rad(rot.X),math.rad(rotY),math.rad(rot.Z))}
local propertyTable = {
CFrame = nextNode.CFrame
}
local tween = TweenService:Create(self.Carriages[carriageNumber],info,position)
tween:Play()
tween.Completed:Wait()
end
return TrainService
Any help would be appreciated!
