Position & orientation saving system not working

hello, i am working on a game and i need to save all items player has placedd down on their base. to do that i am saving the position and orientation, and then put it into a cframe, so i can use SetPrimaryPartCFrame, but it doesnt work. here is a video and the script

local PlotsDS = game:GetService("DataStoreService"):GetDataStore("Plots")
local StructureDS = game:GetService("DataStoreService"):GetDataStore("Structures")
local itemsmodule = require(game.ServerScriptService.ItemsModule)

game.Players.PlayerAdded:Connect(function(plr)
	local plots = PlotsDS:GetAsync(plr.UserId)
	local structures = StructureDS:GetAsync(plr.UserId)
	local foundplot = false
	local spawnpart
	for i,v in pairs(workspace.Plots:GetChildren()) do
		if v.Owner.Value == "No Owner" then
			v.Owner.Value = plr.Name
			spawnpart = v.SpawnPos
			foundplot = true
			for i,b in pairs(game.ServerStorage.BaseExpansions:FindFirstChild(v.Name):GetChildren()) do
				if table.find(plots,b.Name) then
					b.Parent = v.BaseParts
					for i,c in pairs(b:GetChildren()) do
						if c.Name == "BuildArea" then
							c.Parent = v.BuildAreas
						end
					end
				end
			end
			for i,g in pairs(structures) do
				local structure = itemsmodule.ItemFromTable(plr,g[1])
				structure.Parent = v.Structures
				local scframe = CFrame.new(v.Middle.Position + Vector3.new(g[2])) * CFrame.Angles(math.rad(g[3][1]),math.rad(g[3][2]),math.rad(g[3][3])) 
				structure.Parts:SetPrimaryPartCFrame(scframe)
			end
			break
		end
	end
	if foundplot == false then
		plr:Kick("Sorry, The Server Is Full. Please Rejoin.")
	end
	plr.CharacterAdded:Connect(function(char)
		char:SetPrimaryPartCFrame(spawnpart.CFrame)
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local plots = {}
	local structuretable = {}
	local foundbase = false
	for i,v in pairs(game.Workspace.Plots:GetChildren()) do
		if v.Owner.Value == plr.Name then
			v.Owner.Value = "No Owner"
			for i,g in pairs(v.Structures:GetChildren()) do
				local infotable = {}
				local iteminfotable = itemsmodule.ItemToTable(g)
				table.insert(infotable,iteminfotable)
				local vector3V1 = v.Middle.Position-g.Parts.PrimaryPart.Position
				local posX = -(g.Parts.PrimaryPart.Position.X - v.Middle.Position.X)
				local posY = g.Parts.PrimaryPart.Position.Y - v.Middle.Position.Y
				local posZ = -(g.Parts.PrimaryPart.Position.Z - v.Middle.Position.Z)
				local vector3V2 = g.Parts.PrimaryPart.Orientation
				table.insert(infotable,{posX,posY,posZ})
				table.insert(infotable,{vector3V2.X,vector3V2.Y,vector3V2.Z})
				table.insert(structuretable,infotable)
				g:Destroy()
			end
			v.Structures:ClearAllChildren()
			for i,b in pairs(v.BaseParts:GetChildren()) do
				if b.Name ~= "BasePart1" then
					table.insert(plots,b.Name)
					b.Parent = game.ServerStorage.BaseExpansions:FindFirstChild(v.Name)
					for i,c in pairs(v.BuildAreas:GetChildren()) do
						if c.BaseNumber.Value == b.BaseNumber.Value then
							c.Parent = b
						end
					end
				end
			end
			foundbase = true
			break
		end
	end
	if foundbase == true then
		local s,e = pcall(function()
			PlotsDS:SetAsync(plr.UserId,plots)
		end)
		if s then
			print("zapisano dzialki")
		else
			warn(e)
			print("nie zapisano dzialek")
		end
		local s2,e2 = pcall(function()
			StructureDS:SetAsync(plr.UserId,structuretable)
		end)
		if s2 then
			print("zapisano struktury")
		else
			warn(e2)
			print("nie zapisano struktur")
		end
	end
end)
2 Likes

I don’t get your concept here, how can you put a position and orientation into a cframe? you mean make a new cframe? if you’re making a new cframe that cframe won’t affect the model unless the model’s primary part cframe is changed.

But i have worked on a similar type of system, and what i did was make a new model for every player and had an invisible primary part in the centre. So when the player places a block/part it goes into the model, so now you can easily just change the cframe of the primary part of the model!

1 Like

each item has a primary part called hitbox. thats what i use to move the part

local scframe = CFrame.new(v.Middle.Position + Vector3.new(g[2])) * CFrame.Angles(math.rad(g[3][1]),math.rad(g[3][2]),math.rad(g[3][3])) 
				structure.Parts:SetPrimaryPartCFrame(scframe)
1 Like

Nice!, but can you tell me why and when you would want to move the part after placing it down?

1 Like

placing works fine, the problem is with saving. when you rejoin they are in the wrong position, and their rotation is messed up

1 Like

oh that’s simple you can just take the cframe of the primary part, you don’t have to make a new one.
something like this:

local model --fill this up
local CFrame = model.PrimaryPart.CFrame

you can take this cframe after the model has been placed down

1 Like

i cant because there are 6 plots in the game, so i need to calculate how far the model is from the middle of the base
i dont know if you understand what i am trying to say, my explaaining is not very good

update: i have fixed the position, the only thing not working is the orientation
image

Nice!, can you show the script?

sure, its something with cframe.angles. i dont think im using it the way i should
also dont mind the PlotsDS, its a different data store, the one im having problems with is StructureDS

local PlotsDS = game:GetService("DataStoreService"):GetDataStore("Plots")
local StructureDS = game:GetService("DataStoreService"):GetDataStore("Structures")
local itemsmodule = require(game.ServerScriptService.ItemsModule)

game.Players.PlayerAdded:Connect(function(plr)
	local plots = PlotsDS:GetAsync(plr.UserId)
	local structures = StructureDS:GetAsync(plr.UserId)
	local foundplot = false
	local spawnpart
	for i,v in pairs(workspace.Plots:GetChildren()) do
		if v.Owner.Value == "No Owner" then
			v.Owner.Value = plr.Name
			spawnpart = v.SpawnPos
			foundplot = true
			for i,b in pairs(game.ServerStorage.BaseExpansions:FindFirstChild(v.Name):GetChildren()) do
				if table.find(plots,b.Name) then
					b.Parent = v.BaseParts
					for i,c in pairs(b:GetChildren()) do
						if c.Name == "BuildArea" then
							c.Parent = v.BuildAreas
						end
					end
				end
			end
			for i,g in pairs(structures) do
				local structure = itemsmodule.ItemFromTable(plr,g[1])
				structure.Parent = v.Structures
				local scframe = CFrame.new(v.Middle.Position + Vector3.new(-g[2][1],g[2][2],-g[2][3])) * CFrame.Angles(math.rad(g[3][1]),math.rad(g[3][2]),math.rad(g[3][3])) 
				structure.Parts:SetPrimaryPartCFrame(scframe)
			end
			break
		end
	end
	if foundplot == false then
		plr:Kick("Sorry, The Server Is Full. Please Rejoin.")
	end
	plr.CharacterAdded:Connect(function(char)
		char:SetPrimaryPartCFrame(spawnpart.CFrame)
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local plots = {}
	local structuretable = {}
	local foundbase = false
	for i,v in pairs(game.Workspace.Plots:GetChildren()) do
		if v.Owner.Value == plr.Name then
			v.Owner.Value = "No Owner"
			for i,g in pairs(v.Structures:GetChildren()) do
				local infotable = {}
				local iteminfotable = itemsmodule.ItemToTable(g)
				table.insert(infotable,iteminfotable)
				local posX = -(g.Parts.PrimaryPart.Position.X - v.Middle.Position.X)
				local posY = g.Parts.PrimaryPart.Position.Y - v.Middle.Position.Y
				local posZ = -(g.Parts.PrimaryPart.Position.Z - v.Middle.Position.Z)
				local vector3V2 = g.Parts.PrimaryPart.Orientation
				table.insert(infotable,{posX,posY,posZ})
				table.insert(infotable,{vector3V2.X,vector3V2.Y,vector3V2.Z})
				table.insert(structuretable,infotable)
				g:Destroy()
			end
			v.Structures:ClearAllChildren()
			for i,b in pairs(v.BaseParts:GetChildren()) do
				if b.Name ~= "BasePart1" then
					table.insert(plots,b.Name)
					b.Parent = game.ServerStorage.BaseExpansions:FindFirstChild(v.Name)
					for i,c in pairs(v.BuildAreas:GetChildren()) do
						if c.BaseNumber.Value == b.BaseNumber.Value then
							c.Parent = b
						end
					end
				end
			end
			foundbase = true
			break
		end
	end
	if foundbase == true then
		local s,e = pcall(function()
			PlotsDS:SetAsync(plr.UserId,plots)
		end)
		if s then
			print("zapisano dzialki")
		else
			warn(e)
			print("nie zapisano dzialek")
		end
		local s2,e2 = pcall(function()
			StructureDS:SetAsync(plr.UserId,structuretable)
		end)
		if s2 then
			print("zapisano struktury")
		else
			warn(e2)
			print("nie zapisano struktur")
		end
	end
end)
1 Like