How could I make it save?

Hello, so I have a game I am working on and you can place tools like a plank and etc. And many people have complained that they dont save when they rejoin which annoyed them. So I was wondering how can I make it save. And I’m not so sure.

For the plank I use a local script in Statergui, the script is very simple. However, there are 9 scripts each for seperate planks (probably not the smartest thing). I do not have much experience with data store, if you can help. Please do! Thank you.

This is the local script:

local plank = game.Workspace.ItemNeeded.PlankNeeded1
local db = false
plank.Touched:Connect(function(Hit)
	if db == false then
		db = true
	end
	if Hit.Parent.Name == "Plank" then
		plank.CanCollide = true
		plank.BillboardGui1:Destroy()
		plank.Transparency = 0
		Hit.Parent:Destroy()
		wait(0.5)
		script:Destroy()
		db = false
	end
end)

Im not quite sure what you are trying to ask, are you trying to save the player’s planks or something else?

I’m trying to save the part position, and make it stay there for a player who has placed it.

Oh ok, so basically when the player joins you want it to load their planks in the same position they placed them?

Yup, I want it to save. I want it to load in the same position.

Alright, I will try to make a basic script and instructions of how to use it, just give me a little bit of time and I’ll get back to you.

Thank you very much for the help!

No problem. Quick question, when you save your ‘planks’ what kind of values are going to be saved to it? (for ex. a build / fight game might have health…)

It isn’t a fighting game. It is more just of an adventure game in a way. And I’m not sure what values. (Sorry if that makes it harder.)

Alright, I’ll just save the name of the block, the material, the color and the transparency then.

There is also CanCollide if you dont mind also adding that.

1 Like

Alright, here is the script:

local DSS = game:GetService("DataStoreService")
local PlayerDataStore = DSS:GetDataStore("PlayerData")

local MaterialData = {
	["Metal"] = Enum.Material.Metal,
	["WoodPlanks"] = Enum.Material.WoodPlanks,
	["Wood"] = Enum.Material.Wood,
}

local function PlaceBlocksAccordingToData(Player, Data)
	for i, v in pairs(Data) do
		local Material = MaterialData[v.MaterialCode]
		local Part = Instance.new("Part")
		Part.Anchored = true
		Part.Name = v.Name
		Part.Color = Color3.fromHex(v.HexCode)
		Part.Material = Material
		Part.Transparency = v.Transparency
		Part.CanCollide = v.CanCollide
		Part.Size = Vector3.new(v.SizeX, v.SizeY, v.SizeZ)
		Part.Position = Vector3.new(v.PositionX, v.PositionY, v.PositionZ)
		Part.Parent = workspace:WaitForChild("Placed_Blocks"):WaitForChild(Player.Name)
	end
end

local Placed_Blocks = Instance.new("Folder")
Placed_Blocks.Name = "Placed_Blocks"
Placed_Blocks.Parent = workspace

game.Players.PlayerAdded:Connect(function(Player)
	local Player_Folder = Instance.new("Folder")
	Player_Folder.Name = Player.Name
	Player_Folder.Parent = Placed_Blocks
	
	local Success, Data = pcall(function()
		return PlayerDataStore:GetAsync(Player.UserId)
	end)
	
	warn("Success:", Success)
	warn("Data:", Data)
	if Success and Data then
		PlaceBlocksAccordingToData(Player, Data)
	elseif Success and not Data then
		warn("Unable to get "..Player.Name.."'s data, they are a new player!")
		
	elseif not Success and not Data then
		Player:Kick("Error while connecting to roblox datastore, please rejoin or try again later.")
		error("Unable to get "..Player.Name.."'s data, unable to connect to datastores.")
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	if workspace:WaitForChild("Placed_Blocks"):FindFirstChild(Player.Name) ~= nil then
		local SaveDataTable = {}
		
		local BlocksToSave = workspace:WaitForChild("Placed_Blocks"):FindFirstChild(Player.Name):GetChildren()
		
		for B = 1, #BlocksToSave do
			local BlockTable = {}
			BlockTable.Name = BlocksToSave[B].Name
			BlockTable.HexCode = BlocksToSave[B].Color:ToHex()
			if BlocksToSave[B].Material == Enum.Material.Metal then
				BlockTable.MaterialCode = "Metal"
			elseif BlocksToSave[B].Material == Enum.Material.WoodPlanks then
				BlockTable.MaterialCode = "WoodPlanks"
			elseif BlocksToSave[B].Material == Enum.Material.Wood then
				BlockTable.MaterialCode = "Wood"
			end
			BlockTable.Transparency = BlocksToSave[B].Transparency
			BlockTable.CanCollide = BlocksToSave[B].CanCollide
			BlockTable.SizeX = BlocksToSave[B].Size.X
			BlockTable.SizeY = BlocksToSave[B].Size.Y
			BlockTable.SizeZ = BlocksToSave[B].Size.Z
			BlockTable.PositionX = BlocksToSave[B].Position.X
			BlockTable.PositionY = BlocksToSave[B].Position.Y
			BlockTable.PositionZ = BlocksToSave[B].Position.Z
			SaveDataTable["BlockTable"..B] = BlockTable
		end
		
		warn(SaveDataTable)
		
		local Success, ErrorCode = pcall(function()
			PlayerDataStore:SetAsync(Player.UserId, SaveDataTable)
		end)
		if not Success then
			warn(ErrorCode)
		end
	end
end)

Basically what you want is every time a player places a new part / plank, it sets it to the player’s blocks. (Put the script into ServerScriptService.)

If you have any further questions just let me know, I may not respond as fast as I was recently, but I’ll get to it.

1 Like

Do I delete the local script inside of StaterGui? And there is another thing, I’m not sure if it is a problem or not, bc a tool touches the empty thing and then the tool gets deleted.

I’m not quite sure as I don’t know any of the game’s layout so I can’t really say. But what I recommend is making a building tool (I assume players build planks) and that when it builds, it sets the new block to the player’s folder.

Do you need a building tool script?

No no it’s fine. Do you want me to send the game link, so you get an idea of what I mean?

Sure please! Then I could test the game and see what you mean.

I know it may seem like those other Find The games. But I’m trying to make this a lot more unique.

1 Like

So, where are the planks that are being saved?