Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can anyone help me with my furniture placement system?

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.)

10 comments

0

Why just not store the cframe, you're making it way to complicated?

0

If i store only the CFrame, then the script wouldn't know what the actual furniture the player used is. Also, I forgot to add this into my post, but each player will have a random base when they join. That's why i'm doing it this way, because if I don't, their furniture could spawn at another person's base.

0

You can search in free models for sandbox and see how it's scripted

0

Last issue could be fixed by making one, storing it in serverstorage, and then set it relative to the correct position, but that would be harder

0

Do you have any ideas on how to get a relative position? I've tried that before, but I never quite seemed to get it right.

0

You could just add a value to X,Y,Z coordinates (that's what I meanth) or if you mean the other relative position (to the front of an object) you can simply use cframe lookvector

0

LookVector isn't the way to go, as that would just spawn the objects infront of the lab or at one same position. Your other method seems pretty confusing. What i'm asking is something like Roblox High School, where your furniture will spawn in the same position in your house regardless of where your house is on the world coordinates.

0

My way I did this would be to make a middlepoint and then check the distance between them, don't know if it's possible but Position1 - Position2 might work. and then save that togheter with the rotation

0

After trying so many times, that method doesn't work either. Instead, it just moves the piece of furniture to the bottom left corner for some reason

0

I've now fixed the issue. However, I do appreciate the attempt of you helping me!

1 answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.