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

How do i save a model's position based on the plot its on?

How would i save a model's position based on the plot its on rather than where it is in the world.

What happens is that if a player saves on plot 1 then loads into plot 2, all their stuff is on plot 1 when they are on plot 2

Here is my code:

local datastore = game:GetService("DataStoreService")
local modelDataStore = datastore:GetDataStore("SavePlot")
local noobs = game:GetService("Teams"):FindFirstChild("Noobs")
local items = game.ReplicatedStorage.Items
local bases = game.Workspace.Explorers

game.Players.PlayerAdded:Connect(function(player) --load
	repeat wait() until player.Team ~= noobs
	local teamname = player.Team.Name
	local models = modelDataStore:GetAsync(player.UserId)
	local modelData = modelDataStore:GetAsync(player.UserId)
	for _, modelData in pairs(models) do
		local targetModel = modelData.ModelName
		local information = modelData.Position
		local newModel = items:FindFirstChild(targetModel):Clone()
		local position = Vector3.new(information[1], information[2], information[3])
		local lookVector = Vector3.new(information[4], information[5], information[6])
		newModel:SetPrimaryPartCFrame(CFrame.new(position, position + (lookVector * 5)))
		newModel.Parent = bases:FindFirstChild(teamname).ItemHolder
	end
end)

game.Players.PlayerRemoving:Connect(function(player) --save
	local team = player.Team
	local teamname = team.Name
	if player.Team ~= noobs then
		local models = {}
		for _, model in pairs(bases:FindFirstChild(teamname).ItemHolder:GetChildren()) do
			local modelData = {}		
			local primaryPart = model.PrimaryPart
			local position = primaryPart.Position
			local direction = primaryPart.CFrame.lookVector 
			modelData.ModelName = model.Name
			modelData.Position = {position.X, position.Y, position.Z, direction.X, direction.Y, direction.Z}
			models[#models + 1] = modelData
			print(modelData.ModelName)
		end
		modelDataStore:SetAsync(player.UserId, models)
		print('done')
	else
		print("player is on starter team and has nothing loaded to save")
	end
end)

1 comment

0

Just for the record the plots are arranged in a "+" facing the center and are all sized (78, 1, 42) if that helps

1 answer

✓ Accepted answer

Well I'm guessing your game is set up somewhat like a Tycoon where each person has their own plot of land - assuming that, all you really would have to do is put all the plot points inside of a model (not parented to the workspace yet of course), and then use MoveTo or SetPrimaryPartCFrame to get the model and all it's contents to the middle of the new plot - then you would likely have to edit the final position for if it appears too low or too high.

2 comments

0

i have no idea how to do that could you give me an example

0

local model = {all your instances}, model:MoveTo(Vector3.new(x,y,z)) OR set the model's PrimaryPart to the very center of the original plot - then model:SetPrimaryPartCFrame(new_plot_center.CFrame) even model:SetPrimaryPartCFrame(CFrame.new(new_plot_center.Position))

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