I need to save player builds.
-
What do you want to achieve? I need to save player builds through a datastore.
-
What is the issue? Pretty self explanatory… when you are teleported, even though I created a datastore which saves the blocks, but it doesn’t work.
-
What solutions have you tried so far? Q: Did you look for solutions on the Developer Hub? A: Yes. I have found nothing that helps me.
I have a folder in workspace that create a folder every time a new player joins and then the blocks they place in the folder that had the players name.
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("InstanceSave")
local keyPrefix = "Player: "
local parts = workspace.PlayersBlocks
local function save(player: Player)
local key = keyPrefix..tostring(player.UserId)
local data = {}
local partsFolder = parts:WaitForChild(tostring(player.Name))
for i, obj: Part in pairs(partsFolder:GetChildren()) do
table.insert(data, {
obj.Name,
obj.CFrame.Position.X,
obj.CFrame.Position.Y,
obj.CFrame.Position.Z,
obj.Color,
obj.Orientation.X,
obj.Orientation.Y,
obj.Orientation.Z
})
end
local success, err
repeat
success, err = pcall(function()
dataStore:UpdateAsync(key, function()
return data
end)
end)
task.wait()
until success
if not success then
warn("Failed to save data "..err)
end
end
local function load(player: Player)
local key = keyPrefix..tostring(player.UserId)
local success, err
local data
repeat
success, err = pcall(function()
data = dataStore:GetAsync(key)
end)
until success or not players:FindFirstChild(player.Name)
if not data then return end
if success then
for i, obj in ipairs(data) do
local newPart = Instance.new("Part")
newPart.Name = obj[1]
newPart.CFrame = CFrame.new(obj[2], obj[3], obj[4])
newPart.Color = obj[5]
newPart.CFrame *= CFrame.Angles(obj[6]*math.pi/180, obj[7]*math.pi/180, obj[8]*math.pi/180)
newPart.Anchored = true
newPart.TopSurface = Enum.SurfaceType.SmoothNoOutlines
newPart.Parent = workspace
end
else
warn("Failed to load data: "..tostring(err))
end
end
players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)
game:BindToClose(function()
for i, plr: Player in ipairs(players:GetPlayers()) do
save(plr)
end
end)
Please tell me what I can do to fix this. Thank you!