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