Hello! As of now, i'm working on a sort of RPG based game. I've got everything sorted out and working, except for one thing: the furniture system.
Everything works well, players can place furniture, it can be bought, etc, but the datastore is the main problem. What's happening is whenever someone joins back in the game and their furniture is being placed back, it always goes to the center of their base no matter where it is placed. If anyone is kind enough to help me with this situation, it would be greatly appreciated!
Also, do note, each player will have a different base when they join the game. If I just store the CFrame and load it back unchanged, their furniture would spawn at some other person's base, or the location of their previous base in the past server.
Here is the code:
Saving the data
local placedItems = game:GetService("DataStoreService"):GetDataStore("PlacedItemsDataStore2") function SavePlacedItems(plr) local items = {} local theItems = workspace.Labs:FindFirstChild(plr.Name).PlacedFurniture local baseCFrame = theItems.Floor.CFrame:toObjectSpace(theItems:GetPrimaryPartCFrame()) local basePos,baseRot = baseCFrame:toAxisAngle() table.insert(items,{"Floor",basePos.x,basePos.y,basePos.z,baseRot}) for i,v in pairs(theItems:GetChildren()) do if(v:IsA("Model")) then local worldCF = v:GetPrimaryPartCFrame():toObjectSpace(theItems:GetPrimaryPartCFrame()) local pos,rot = worldCF:toAxisAngle() table.insert(items,{v.Name,pos.x,pos.y,pos.z,rot}) end end placedItems:SetAsync(plr.UserId,items) end
Loading the data
local placedItems = game:GetService("DataStoreService"):GetDataStore("PlacedItemsDataStore2") local pItems = placedItems:GetAsync(plr.UserId) if(pItems ~= nil) then local baseDat =pItems[1] base.CFrame=CFrame.fromAxisAngle(Vector3.new(baseData[2],baseData[3],baseData[4]),baseData[5]):toWorldSpace(furnThing:GetPrimaryPartCFrame()) for i=2,#pItems do local v = pItems[i] local furn = game.ServerStorage.Furniture:FindFirstChild(v[1]):Clone() furn.Parent = furnThing furn:SetPrimaryPartCFrame(CFrame.fromAxisAngle(Vector3.new(v[2],v[3],v[4]),v[5]):toWorldSpace(furnThing:GetPrimaryPartCFrame())) end furnThing:SetPrimaryPartCFrame(lab.Floor.CFrame) end
(Yes, I know it appears messy, it looks much better in studio, trust me.)
I've managed to fix my issue. I looked at a copy of Roblox High School, and changed my code to look like this:
Saving
local items = {} local lab = workspace.Labs:FindFirstChild(plr.Name) local theItems = lab.PlacedFurniture for i,v in pairs(theItems:GetChildren()) do if(v:IsA("Model")) then local vector,axis = v:GetPrimaryPartCFrame():toAxisAngle() local pos = v.PrimaryPart.Position - lab.Floor.Position table.insert(items,{v.Name,pos.x,pos.y,pos.z,axis}) end end placedItems:SetAsync(plr.UserId,items)
Loading
for i=1,#pItems do local v = pItems[i] local furn = game.ServerStorage.Furniture:FindFirstChild(v[1]):Clone() furn.Parent = furnThing furn:SetPrimaryPartCFrame(lab.Floor.CFrame + Vector3.new(v[2],v[3],v[4])) furn:SetPrimaryPartCFrame(furn:GetPrimaryPartCFrame() * CFrame.Angles(0,v[5],0)) end
As you can see, it's alot less messy and alot more clean.
Now, if any other person out there is stressing over this, feel free to copy my code. I understand your struggle as I was in your shoes.