Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Saved models facing Right, but rotates to face Left on loading in?

Asked by
Cuvette 246 Moderation Voter
7 years ago
Edited 7 years ago

This is a bit of a long one, so ill try to explain it as best as possible. So i'm working on a free build Tycoon style game like miners haven. When a players leaves, the game saves the location of each object (model) and the rotation values.

When the game is next loaded, the players objects are loaded into their plot of land into the positions they were left in. Everything is working, even the rotation values. Apart from if the model is facing east (right), occasionally everything that's facing right will end up facing left when loaded in.

I've looked into the saving part and sometimes the models save with a Y rotation of 1 instead of 0. I'm not quite sure why, because if I play the game through studio and it saves with a value of 1, I can check the model properties in game and it says 0.

Ill post snippets of the script on the saving and loading end so you get a rough idea of what's happening.

for i = #objectNames, 1, -1 do
    Item = game.ReplicatedStorage.Items[objectNames[i]]:Clone()
    print(objectNames[i])
    coord1 = objectX[i]
    coord2 = objectY[i]
    coord3 = objectZ[i] 
    roty = objectRot[i]
    Item.PrimaryPart = Item.Hitbox
    Item:SetPrimaryPartCFrame(CFrame.new(coord1,coord2,coord3) * CFrame.Angles(0, math.rad(roty), 0))
    Item.Parent = game.Workspace.Tycoons[playerTycoon].Objects
end

As you can see from above, i'm loading up the properties of the primary part of each model. Specifically the X,Y,Z and Rotation Y (I don't need the X or Z for rotation). The loading script loads up all of the values in order (This is just a pre-message before you wonder what the tables are doing below.)

For the saving part, ill post the main snippet of my script below.

    objectNames = {}
    objectX = {}
    objectY = {}
    objectZ = {}
    objectRot = {}
    findTycoon = tostring(game.Players[player.Name].PlayerTycoon.Value)
    findItems = game.Workspace.Tycoons[findTycoon].Objects:GetChildren()
    for i = #findItems, 1, -1 do
    table.insert(objectNames,i,findItems[i].Name)
    table.insert(objectX,i,findItems[i].Hitbox.Position.X)
    table.insert(objectY,i,findItems[i].Hitbox.Position.Y)
    table.insert(objectZ,i,findItems[i].Hitbox.Position.Z)
    if findItems[i].Hitbox.Rotation.Y ~= 1 or findItems[i].Hitbox.Rotation.Y ~= -1 then
        table.insert(objectRot,i,math.ceil(findItems[i].Hitbox.Rotation.Y)) 
    end
    wait()
    if findItems[i].Hitbox.Rotation.Y >=1 and findItems[i].Hitbox.Rotation.Y <= 5 then
        table.insert(objectRot,i,math.ceil(0))  
    end

This part grabs the X,Y,Z and Rotation Y, it then inserts them into separate tables. As you can see I've tried to solve the problem by changing the value of the Y Rotational value if it's above or equal to 1 and lower or equal than 5. This still doesn't help because the primary part still saves with a value of 1.

For more of an understanding, I printed the location values and rotation value after every loop so I could see if it was saving properly, in which it wasn't

Here's the print in the output I got from one of the models.

-- Object Name, X, Y, Z, Y Rotation
Straight Conveyor -58 3 81 1

Some of them are still saving as 1. This isn't possible because I've fool proofed it in the saving script.

Anyway, if anyone has any idea or if you need any more just comment. Thanks!

0
Why are you using the ceil function? Lets say the model is rotated slightly, with a Y-axis rotation of 0.004. This means that you save math.ceil(0.004) = 1. Because of floating point errors, it is not uncommon for numbers to be slightly off from what you expect them to be. Is there some reason that integer percision is required? BlackJPI 2658 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Try setting (and storing) the CFrame of each of your parts so that it is rotated properly, and then when you to go insert the tycoon's models, use the MoveTo function of Model to move your model into the proper position.

Example:

local cframes = {} -- you might want to make this a dictionary with the types of tycoon objects, and the cframes.
for i,v in pairs(model) do
    if v:IsA("BasePart") then
        table.insert(cframes, v.CFrame)
    end
    if v:IsA("Model") then
        table.insert(cframes, v.CoordinateFrame)
    end
end
Ad

Answer this question