I've managed to make something that works well with your script and its "plug in play" meaning you simply put your model in the Spinner group and the script takes care of the rest.
If you want to control where it rotates from just update the Model.PrimaryPart and the position from your spinner part, But this is optional as i've included a PrimaryPart Finder.
The primary part Finder works out the centre of your model and sets the closest Part to that point as Model.PrimaryPart. If the Model.PrimaryPart is set it simply returns/welds everything to that part!, Cool huh?
The only downside is everything in the model will be frozen/welded to that Primary part.
Edit:
Changed the code to include a Timer set to 60 secs and a way of easily changing the rotation direction and how much it rotates by. Like in your example you had it rotating by 10 rotation points. You can now change that by adjusting the SpinStep
I've also included a relatively simple way of resetting the rotation back to how it was when you first started. Though I wouldn't be surprised if it doesn't break lol. It was an afterthought lol xD
The Updated Code:
local DoReset = true
local SpinRotation = "z"
local SpinStep = 10
local RotationTime = 60
local SpinnerGroup = workspace:FindFirstChild("Spinner")
if(SpinnerGroup == nil) then error("Spinner Group is nil! and cannot be found!") end
local Axel = SpinnerGroup:FindFirstChild("moving")
if(Axel == nil) then error("Axel is nil! and cannot be found!") end
local ModelsToRotate = (function()
local Models = {}
local ModelObjs = SpinnerGroup:GetChildren()
local getPrimaryPartfrom = function(mod)
local PP = mod.PrimaryPart
local ModelCenter = Vector3.new()
local ModelParts = {}
local X,Y,Z = 0,0,0
if(PP == nil) then
local Parts = mod:GetDescendants()
local Num = 0
for k,v in pairs(Parts) do
if(v:IsA("BasePart") == true) then
local Pos = v.Position
X = X + Pos.X
Y = Y + Pos.Y
Z = Z + Pos.Z
Num = Num + 1
ModelParts[#ModelParts+1] = v
end
end
X = X/Num
Y = Y/Num
Z = Z/Num
ModelCenter = Vector3.new(X,Y,Z)
end
local Dist = math.huge
for k,v in pairs(ModelParts) do
local dist = (ModelCenter - v.Position).magnitude
if(dist < Dist) then
Dist = dist
PP = v
end
end
return PP
end
for k,model in pairs(ModelObjs) do
if(model:IsA("Model") == true) then
local PPart = getPrimaryPartfrom(model)
Models[#Models+1] ={
Model = model,
PrimaryPart = PPart
}
model.PrimaryPart = PPart
end
end
return Models
end)()
for k,mod in pairs(ModelsToRotate) do
local Model = mod.Model
local BasePart = mod.PrimaryPart
local Parts = Model:GetDescendants()
for k,part in pairs(Parts) do
if(part:IsA("BasePart")== true) then
local Weld = Instance.new("WeldConstraint",BasePart)
Weld.Part0 = BasePart
Weld.Part1 = part
if(part.Anchored == true) then
part.Anchored = false
end
end
end
local MainWeld = Instance.new("WeldConstraint",BasePart)
MainWeld.Part0 = Axel
MainWeld.Part1 = BasePart
end
local function getOrientation(CF)
local sx, sy, sz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = CF:components()
local Y = math.atan2(m02, m22)*(180/math.pi)+180
local X = math.asin(-m12)*(180/math.pi)+180
local Z = math.atan2(m10, m11)*(180/math.pi)+180
return Vector3.new(X,Y,Z)
end
local t = 0
local Clicked = false
local SwitchedOff = false
local ClickDB = false
local oldsecs = 0
local OriginalRot = getOrientation(Axel.CFrame)
function SetAngles(newRot)
if(newRot == nil) then newRot = SpinRotation end
newRot = string.lower(newRot)
if(newRot == "x") then return CFrame.Angles(math.rad(SpinStep),0,0) end
if(newRot == "-x") then return CFrame.Angles(-math.rad(SpinStep),0,0) end
if(newRot == "y") then return CFrame.Angles(0,math.rad(SpinStep),0) end
if(newRot == "-y") then return CFrame.Angles(0,-math.rad(SpinStep),0) end
if(newRot == "z") then return CFrame.Angles(0,0,math.rad(SpinStep)) end
if(newRot == "-z") then return CFrame.Angles(0,0,-math.rad(SpinStep)) end
end
local function math_floor(V3)
return Vector3.new(math.floor(V3.X)+0.5,math.floor(V3.Y)+0.5,math.floor(V3.Z)+0.5)
end
script.Parent.Color = Color3.fromRGB(0,255,0)
script.Parent.ClickDetector.MouseClick:connect(function(player)
Clicked = not Clicked
if(Clicked == true) then
script.Parent.Color = Color3.fromRGB(255,255,0)
else
SwitchedOff = true
script.Parent.Color = Color3.fromRGB(255,0,0)
end
while Clicked == true and SwitchedOff == false do
if(Clicked == false) then break end
local GetTo = RotationTime * 30
local secs = math.floor((t /30)+0.5)
if(secs ~= oldsecs) then
print("Seconds Running: "..secs)
end
oldsecs = secs
local cf = SetAngles()
Axel.CFrame = Axel.CFrame * cf
if(t <= GetTo) then t = t +1 else Clicked = false t = 0 break end
wait()
end
if(SwitchedOff == true and Clicked == false) then
if(DoReset == true) then
while SwitchedOff == true do
local bHasDash = SpinRotation:find("-") ~= nil
local ReverseDir = bHasDash == true and SpinRotation:sub(2,2) or "-"..SpinRotation
local cf = SetAngles(ReverseDir)
local MyRot = getOrientation(Axel.CFrame)
if(math_floor(MyRot) ~= math_floor(OriginalRot)) then
Axel.CFrame = Axel.CFrame * cf
else
SwitchedOff = false
end
wait()
end
end
SwitchedOff = false
end
t = 0
wait(1)
script.Parent.Color = Color3.fromRGB(0,255,0)
print("Done!")
end)
How to use:
Simply move your models that you want to rotate inside the Spinner Group and the script will take care of the rest!. Just make sure you're happy with the position and starting rotation!
Edit:
So you should be able to copy and paste this into your spinner code and set the groups and axel properly and any model(s) you want to rotate, Plus the rotation direction And Rotation step. Breath
So you should be good to go!
Hope this helps and if you have any problems let me know!
Peace! :)
1 comment
hmm... ill see if i can come up with something...