Game crashes when part reaches certain CFrame

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.

1 Like

How can I fix this problem then?

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.

I get what you are saying, the primarypart won’t be exactly the same position, but I have no idea AT ALL how to do what you are recommending.

     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
1 Like

This was the least “tedious” solution I could think of…

Try to add wait() to the while loop

It wouldn’t really matter since the first while loop is yielded by the one inside it.

How can I fix this?
Workspace.Att.Sleigh:15: invalid argument #1 to 'floor' (number expected, got Vector3) - Server - Sleigh:15

Updated the code so that it fits now.

Sadly crashed. Script timeout: exhausted allowed execution time

I forgot it was for the end. Updated the code, should work now.

I don’t see a break statement for the main while true do loop, is it supposed to break or run infinitely making it crash?

it is supposed to just make circles around the map forever

Oh you mean the Game crashes not the actual sleigh??

Yep, the game crashes. I’m not sure how to fix it.

       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

try this.

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.