I am trying to create a sleigh that will circle around my map, but when it reaches the first attachment it crashes. I’m not sure why exactly. Any help would be appreciated.
Code:
local TweenService = game:GetService("TweenService")
local timea = time()
local total = 20
while true do
local att = 1
local model = workspace.Sleigh
local start = model:GetPrimaryPartCFrame()
while time() - timea <= total do
local End = game.Workspace.Att["Attachment"..att].WorldCFrame
local TI = TweenService:GetValue((time() - timea)/total, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local CFrame2 = start:Lerp(End,TI)
model:SetPrimaryPartCFrame(CFrame2)
model:SetPrimaryPartCFrame(CFrame.lookAt(model.PrimaryPart.Position, script.Parent.Attachment1.WorldPosition))
if model.PrimaryPart.CFrame == End then
att = att + 1
local total = 20
local timea = time()
end
wait()
end
end
This is because the primarypart’s CFrame is highly highly unlikely to be exactly the attachment’s CFrame as you are doing the tween over and over again.
Do a runtime variable and use that to check which attachment it should be on. If the runtime total is larger than the sum of the total tweens’ time then this is where modulus “%” is useful.
local TweenService = game:GetService("TweenService")
local timea = time()
local total = 20
while true do
local att = 1
local model = workspace.Sleigh
local start = model:GetPrimaryPartCFrame()
while time() - timea <= total do
local End = game.Workspace.Att["Attachment"..att].WorldCFrame
local TI = TweenService:GetValue((time() - timea)/total, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local CFrame2 = start:Lerp(End,TI)
model:SetPrimaryPartCFrame(CFrame2)
model:SetPrimaryPartCFrame(CFrame.lookAt(model.PrimaryPart.Position, script.Parent.Attachment1.WorldPosition))
if math.floor((model.PrimaryPart.CFrame.Position-End.Position).Magnitude) == 0 then
att = att + 1
local total = 20
local timea = time()
end
wait()
end
end
local TweenService = game:GetService("TweenService")
local timea = tick()
local total = 20
while true do
local att = 1
local model = workspace.Sleigh
local start = model:GetPrimaryPartCFrame()
while tick() - timea <= total do
local End = game.Workspace.Att["Attachment"..att].WorldCFrame
local TI = TweenService:GetValue((time() - timea)/total, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local CFrame2 = start:Lerp(End,TI)
model:SetPrimaryPartCFrame(CFrame2)
model:SetPrimaryPartCFrame(CFrame.lookAt(model.PrimaryPart.Position, script.Parent.Attachment1.WorldPosition))
if model.PrimaryPart.CFrame == End then
att = att + 1
local total = 20
local timea = tick()
end
wait()
end
end
It just crashes the game after like 5 seconds, only thing it does is rotate towards the attachment. No errors apart from the exhauster execution time one
That is very strange… because that means tick() - timea <= total is returning false.
local TweenService = game:GetService("TweenService")
local timea = tick()
local total = 20
while true do
local att = 1
local model = workspace.Sleigh
local start = model:GetPrimaryPartCFrame()
while tick() - timea <= total do
local End = game.Workspace.Att["Attachment"..att].WorldCFrame
local TI = TweenService:GetValue((time() - timea)/total, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local CFrame2 = start:Lerp(End,TI)
model:SetPrimaryPartCFrame(CFrame2)
model:SetPrimaryPartCFrame(CFrame.lookAt(model.PrimaryPart.Position, script.Parent.Attachment1.WorldPosition))
if model.PrimaryPart.CFrame == End then
att = att + 1
local total = 20
local timea = tick()
end
wait()
end
wait()
end
I think it’d be sensible to add a delay to the 2nd loop.