i am making a roblox survival island game similar to Stranded Deep and i want every impact you make on the game to stay like that. Ex: Craft a shelter, cut a tree, pick up rocks and sticks from the beach etc., so it saves and next time the player enters the changes remain, including the player spawning at the exact same position he left.
You could loop through all the parts in workspace and then safe the position of them. When the player joins, you clone these objects from serverstorage and then you place them to the position.
I don’t know if this is the best approach for this though.
Hello,
Well, I would do it similarly, but let’s start with the simplest thing: the player spawnpoint.
First we create two functions that make loading and saving easier
local function SaveData(plr : Player, DataToSave : string)
local Key = "Key_"..plr.UserId
local DataStore = DataStoreService:GetDataStore("YourDataStoreName1", Key)
local sucess, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId, DataToSave)
end)
end
and now to load Data
local function LoadData(plr : Player)
local Key = "Key_"..plr.UserId
local DataStore = DataStoreService:GetDataStore("YourDataStoreName1", Key)
local success, currentValue = pcall(function()
return DataStore:GetAsync(plr.UserId)
end)
if success then
return currentValue
else
return false
end
end
now we can write the Code to Save and set the Position on load
Players.PlayerAdded:Connect(function(plr : Player)
local PlayerData = LoadData(plr)
if PlayerData then
-- Here we can do something with the Code
end
plr.CharacterAdded:Connect(function(Char)
if PlayerData then
local DecodedData = Http:JSONDecode(PlayerData)
if DecodedData then
Char:MoveTo(Vector3.new(DecodedData.pos_X, DecodedData.pos_Y, DecodedData.pos_Z))
end
end
end)
plr.CharacterRemoving:Connect(function(Char )
local CharPosition = Char.PrimaryPart.Position
local TableToSave = {pos_X = CharPosition.X, pos_Y = CharPosition.Y, pos_Z = CharPosition.Z}
local JsonString = Http:JSONEncode(TableToSave)
SaveData(plr, JsonString)
end)
end)
Now we can concentrate on saving all objects.
Unfortunately, I don’t know how you save the individual objects, but I assume the following setup:
MainFolder → SubFolders for object types → ObectsToSave in Folder
Of course, your structure may be different, so I’ll explain how you could do it.
Players.PlayerRemoving:Connect(function(plr : Player)
local MainTableToSave = {}
for _, SubFolder in pairs(workspace.ObjectsToSave:GetChildren()) do
local TableToSave = {}
for _, Obj : Model in pairs(SubFolder:GetChildren()) do
local PositionToSave
if Obj:IsA("Model") then
local Pos = Obj:GetPivot()
PositionToSave = {pos_X = Pos.X, pos_Y = Pos.Y, pos_Z = Pos.Z}
elseif Obj:IsA("MeshPart") then
PositionToSave = {pos_X = Obj.Position.X, pos_Y = Obj.Position.Y, pos_Z = Obj.Position.Z}
end
local TableToInstert = {Name = Obj.Name, Position = PositionToSave}
table.insert(TableToSave, TableToInstert)
end
MainTableToSave[SubFolder.Name] = TableToSave
end
local EncodedString = Http:JSONEncode(MainTableToSave)
SaveData(plr, "DIFFERENT_DATA_STORE_NAME", EncodedString) -- Here we are Saving all Folder
end)
Now we can access the first function “PlayerAdded” again and after the If query load the data from the data store and place it on the world or we do it directly when the server and the game starts - it depends on which system you are using and how you want to save or load the data.
Important: You can only save a table in a JSON string, which means that when you retrieve it again you have to convert it back to a LUA table.
I would have all objects in the ReplicatedStorage or ServerStorage so that the player does not have to preload objects he does not need. as soon as the player enters the game or if it is only a 1 player game I would load all objects with a script based on the memory of the player and position the objects.
On a side note, you might run into datastore storage limitis really quickly doing this, so I would try to only store what’s necessary: only store player made changes, and consider storing them in a buffer to save on as much space as possible
That will be cool to played it when it comes out
I can only agree with you here, I didn’t pay attention to the maximum size of a key. The smartest option here would be to only save what we have actually changed if a tree is still in the same place and has not been destroyed, then we don’t have to load it.