Why cant it just PIVOT!

Im trying to make a dice rolling script but it freezez at the part where I make it repeat the pivot until it works and then it says


local TweenService = game:GetService("TweenService")

local module = {}

function module.Roll(Dice)
	print(Dice)
	
	local og = Dice.OCF
	
	for count = 1, 100 do
		
		Dice.Model:PivotTo(CFrame.new(CFA.Position, Vector3.new(math.random(-100, 100), math.random(-100, 100), math.random(-100, 100))))
		task.wait(math.random(3, 5)/100)
	end
	
	task.wait(0.02)
	
	local Roll = math.random(1, #Dice.RollCFrames:GetChildren())
	
	repeat
		Dice.Model:PivotTo(og.Value)
		print(og.Value)
	until Dice.Model.PrimaryPart.CFrame == og.Value
	
	repeat
		Dice.Model:PivotTo(Dice.RollCFrames:FindFirstChild(tostring(Roll)).Value)
		print(Dice.RollCFrames:FindFirstChild(tostring(Roll)).Value)
	until Dice.Model.PrimaryPart.CFrame == Dice.RollCFrames:FindFirstChild(Roll).Value
	
	
	print(Dice.RollCFrames:FindFirstChild(Roll).Value)
	
	return Roll
end

return module

Scripts can only run for so long before being forcefully killed by the engine. That error code is implying this script is running too much for the engine to handle and is thus being killed prematurely.

Let’s analyze your script to see where that could be happening:

for count = 1, 100 do		
  Dice.Model:PivotTo(CFrame.new(CFA.Position, Vector3.new(math.random(-100, 100), 
  math.random(-100, 100), math.random(-100, 100))))
  task.wait(math.random(3, 5)/100)
end

Unlikely to be here, since it’s just a simple for loop that executes 100 times.

repeat
  Dice.Model:PivotTo(og.Value)
  print(og.Value)
until Dice.Model.PrimaryPart.CFrame == og.Value

This could be an issue. You are repeating this block of code with no delay until Dice.Model.PrimaryPart.CFrame == og.Value. What if that expression never equals true?

repeat
  Dice.Model:PivotTo(Dice.RollCFrames:FindFirstChild(tostring(Roll)).Value)
  print(Dice.RollCFrames:FindFirstChild(tostring(Roll)).Value)
until Dice.Model.PrimaryPart.CFrame == Dice.RollCFrames:FindFirstChild(Roll).Value

Same issue here as explained above.

Try adding a task.wait() call in each of these repeat until blocks to see if that helps. If it doesn’t, something is preventing either one of these repeat until blocks to close, which is most likely where your problem lies.

Thank you, but the problem is that it the PivotTo function doesn’t work, I cant figure out why.

Try using SetPrimaryPartCFrame instead of PivotTo, or set the pivot of the model to the PrimaryPart’s CFrame outside of run time.

1 Like

Instead just use Random.new():NextUnitVector(), then multiply it by 100, It should give you a Random Direction.

So you want him to use a Deprectated function instead of its successor?

Thanks for the tip, it helps make the code look so much cleaner, I’ll also remember this for future instances that that function exists.

Also, that function might be deprecated, but it does seem to fix the problem.

I have absolutely no idea why the deprecated version of PivotTo() seems to work instead of the updated version, thank you for helping me, this project has been driving me insane.

kinda in depth explanation why it works, and better alternatives:

This is exactly what’s happening. When you use PivotTo, it sets the model’s Pivot to the desired location. model’s Pivot, not PrimaryPart CFrame. An easy fix would be to just directly set the PrimaryPart’s CFrame using SetPrimaryPartCFrame. That way so, when it is set, the condition holds true and it doesn’t get stuck in an infinite loop.

However there’s a few other ways around this problem, one of which being to manually set the Pivot of the model to the PrimaryPart’s CFrame. This way when you use PivotTo, it sets the Pivot of the model, but since the Pivot is the same as the PrimaryPart’s CFrame, the CFrame should also equal the desired value, making the expression true.

Another way would to just check the Pivot of the model instead of the PrimaryPart’s CFrame, like this:

until Dice.Model.WorldPivot == og.Value 
--worldpivot is the pivot of the model relative to the world (or 0,0,0)
--basically the model's cframe

However, all the solutions above are just bandaids to the underlying root of the problem. Actually, you just had to ditch the unnecessary repeats. You’re trying to set the CFrame, not gradually perform a makeshift tween.
What you’re doing is like this:

local something = true

repeat
something = false
until something == false

Instead of just doing something = false.

The reason your original code didn’t work was because the Pivot must’ve not been equal to the PrimaryPart’s CFrame. So whenever you set one of them, the other would always have a little bit of offset. Because of this offset, both of them could never be equal to each other. So if you set one, and checked the other one expecting it’s CFrame to be the same, it would always hold out false.

If you need anymore help or clarification on anything, feel free to reach out to me. I’m always down to help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.