I simply wanted to create a stored model from Replicated Storage to the workspace. I moved the model just fine but when I rotate it, it always comes out completely different than what is needed. I used the same lines of code on my cannon (lower) and the cannon comes out exactly at its 90* like programmed to do. Both my cannon and ship models are zeroed in orientation while stored but the ship always randomly rotates to a random number that’s not assigned to it, except 0.
Everything is working fine except the rotation of the newly clonned ship.
local but = script.Parent.Parent.StartButton
local clic = but.ClickDetector
local debounce = false
local Playerz = game.Players
local rep = game:GetService("ReplicatedStorage")
local Models = rep.ShipModels
--------------------------------------------
local x = script.Parent.Position.X
local y = script.Parent.Position.Y
local z = script.Parent.Position.Z
------------------------------------------
---------ShipSpawn----------------
but.ClickDetector.MouseClick:Connect(function(plr)
if debounce == false then
debounce = true
local leader = plr:FindFirstChild("leaderstats")
local shipl = leader.ShipLevel.Value
local cannonl =leader.CannonLevel.Value---getting player level values
local shipmodel = Instance.new("Model",game.Workspace.ShipSpawns)
shipmodel.Name = "Ship"----spawning ship only model
local ship = Models["Ship"..shipl]:Clone()
ship.Parent = game.Workspace.ShipSpawns.Ship--adding the ship model itself to the model
ship:MakeJoints()
ship:SetPrimaryPartCFrame(CFrame.new(x+30, y, z+20))--Position Where the Boat Spawns
ship:SetPrimaryPartCFrame(ship:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,math.deg(90),0))--!!MAIN ISSUE!!
---------ShipSpawn----------------
---------AttachCannon----------------
local cannonQ = Models["Cannon"..cannonl]:Clone()
cannonQ.Parent = ship
local ReferancePartQ = ship.ReferancePartQ
local rqx = ReferancePartQ.Position.X
local rqy = ReferancePartQ.Position.Y
local rqz = ReferancePartQ.Position.Z
cannonQ.PrimaryPart.Anchored = true
local qpp = cannonQ.Base
cannonQ:SetPrimaryPartCFrame(CFrame.new(rqx,rqy+0.5,rqz-1))
cannonQ:SetPrimaryPartCFrame(cannonQ:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,math.deg(90),0))
ship.PrimaryPart.Anchored = true
cannonQ.PrimaryPart.Anchored = false
---------AttachCannon----------------
----------Weld----------
local primarypart = ship.PrimaryPart
local parts = ship:GetChildren()
for i = 1, #parts do
if parts[i]:IsA('BasePart') then
local weld = Instance.new("Weld")
weld.Part0 = primarypart
weld.Part1 = parts[i]
weld.C0 = primarypart.CFrame:inverse()
weld.C1 = parts[i].CFrame:inverse()
weld.Parent = primarypart
end
end
----------Weld----------
wait(10)
debounce = false
end
end)
I can’t say what the issue is, but how have you been debugging this? I debug by using loads of prints.
local but = script.Parent.Parent.StartButton
local clic = but.ClickDetector
local debounce = false
local Playerz = game.Players
local rep = game:GetService("ReplicatedStorage")
local Models = rep.ShipModels
--------------------------------------------
local x = script.Parent.Position.X
local y = script.Parent.Position.Y
local z = script.Parent.Position.Z
------------------------------------------
but.ClickDetector.MouseClick:Connect(function(plr)
if debounce == false then
debounce = true
---------ShipSpawn----------------
local leader = plr:FindFirstChild("leaderstats")
local shipl = leader.ShipLevel.Value
local cannonl =leader.CannonLevel.Value---getting player level values
local shipmodel = Instance.new("Model",game.Workspace.ShipSpawns)
shipmodel.Name = "Ship"----spawning ship only model
local ship = Models["Ship"..shipl]:Clone()
ship.Parent = game.Workspace.ShipSpawns.Ship--adding the ship model itself to the model
ship:MakeJoints()
local new_cframe = CFrame.new(x+30, y, z+20)
ship:SetPrimaryPartCFrame(new_cframe)--Position Where the Boat Spawns
local rotated_cframe = ship:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,math.deg(90),0)
ship:SetPrimaryPartCFrame(rotated_cframe)--!!MAIN ISSUE!!
print('x:', x, ' y:', y, ' z:', z, ' new_cframe:', new_cframe, ' rotated_cframe:', rotated_cframe)
---------ShipSpawn----------------
---------AttachCannon----------------
local cannonQ = Models["Cannon"..cannonl]:Clone()
cannonQ.Parent = ship
local ReferancePartQ = ship.ReferancePartQ
local rqx = ReferancePartQ.Position.X
local rqy = ReferancePartQ.Position.Y
local rqz = ReferancePartQ.Position.Z
cannonQ.PrimaryPart.Anchored = true
local qpp = cannonQ.Base
cannonQ:SetPrimaryPartCFrame(CFrame.new(rqx,rqy+0.5,rqz-1))
cannonQ:SetPrimaryPartCFrame(cannonQ:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(0,math.deg(90),0))
ship.PrimaryPart.Anchored = true
cannonQ.PrimaryPart.Anchored = false
---------AttachCannon----------------
----------Weld----------
local primarypart = ship.PrimaryPart
local parts = ship:GetChildren()
for i = 1, #parts do
if parts[i]:IsA('BasePart') then
local weld = Instance.new("Weld")
weld.Part0 = primarypart
weld.Part1 = parts[i]
weld.C0 = primarypart.CFrame:inverse()
weld.C1 = parts[i].CFrame:inverse()
weld.Parent = primarypart
end
end
----------Weld----------
wait(10)
debounce = false
end
end)
Look at the output; if the rotation changes every time, some of those values have to change. Try pinpoint where it is messing up.
Printing isn’t needed as I just look at the model primary part.
When math.deg(0) the orenatition is (0,0,0)
When math.deg(90) the orenatition is (0, -107.43, 0)
When math.deg(180) the orenatition is (0, 145.14, 0)
When math.deg(1) the orenatition is (0, 42.81, 0)
When math.deg(100) the orenatition is (0, -39.36, 0)
This is the Random or inconsisintcy im talking about
math.deg takes in a radian value and outputs it in degrees, however cframe.euleranglesxyz takes in radians. Instead you should be using math.rad(degrees).