Part Saving And Loading

I am asking if I am doing this correctly

local PartsToSave = {}
local Plots = workspace.Plots

local SaveManager = {}

function SaveManager.Sterilize(player: Player)
	local PlayersPlot = Plots:WaitForChild(player.Name .. " Plot")
	local Objects = PlayersPlot.Objects
	
	for _, Object in ipairs(Objects:GetChildren()) do
		table.insert(PartsToSave, {
			Object.Name,
			Object.Hitbox.CFrame.Orientation.X,
			Object.Hitbox.CFrame.Orientation.Y,
			Object.Hitbox.CFrame.Orientation.Z,
			Objects.Hitbox.CFrame.Position.X,
			Objects.Hitbox.CFrame.Position.Y,
			Objects.Hitbox.CFrame.Position.Z
		})
	end
end

function SaveManager.SaveParts()
	
end

return SaveManager

1 Like

the main issue with your code is that you’re mixing up Object.Hitbox and Objects.Hitbox you got a typo there that’s gonna mess everything up.

Also like, you’re saving the position data in a pretty inefficient way and your PartsToSave table is global which means it’s gonna get messy if multiple players are saving at the same time

Try this code:

local PartsToSave = {} -- maybe make this per player?
local Plots = workspace.Plots

local SaveManager = {}

function SaveManager.Sterilize(player: Player)
    local PlayersPlot = Plots:WaitForChild(player.Name .. " Plot")
    local Objects = PlayersPlot.Objects
    
    local playerData = {} -- separate table for this player
    
    for _, Object in ipairs(Objects:GetChildren()) do
        -- fixed your typo here bro
        table.insert(playerData, {
            Name = Object.Name,
            CFrame = Object.Hitbox.CFrame, -- save the whole CFrame instead of splitting it
            -- etc.
        })
    end
    
    PartsToSave[player.UserId] = playerData -- organize by player
    return playerData -- return it so you can use it right away
end

function SaveManager.SaveParts(player: Player)
    -- actually save to datastore here

end


return SaveManager

1 Like

I will add that if this is for datastore purposes, you can’t store userdata data types like CFrames in them, and you have to store the components of the CFrame instead (which you can get by calling :GetComponents())

2 Likes

Oh, mb, thank you then, didn’t pay attention, lol

2 Likes

I was asking for feedback but thank you for giving me some tips I will dissect this script and see where I went wrong

1 Like

Thanks for the feedback, I will try to use this!

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