So I've been working on a Roblox RPG and for four days I've been trying to make a save and load system. But I've had no success. I'm trying to use a Script in ServerScriptService to give the player a CFrame value and when the player presses a save button, the CFrame updates and when they leave it stays the same. I've looked all over to find any answer but no success in what I'm trying to do. So far this is what I have in my DataStore Script.
local DataStore = game:GetService("DataStoreService") local save = DataStore:GetDataStore("SavePos") game.Players.PlayerAdded:Connect(function(plr) local spot = Instance.new("Folder",plr) spot.Name = "SaveLocation" local pos = Instance.new("CFrameValue",spot) pos.Name = "PlayerPosition" end)
This is all I have, there used to be more but I deleted it since it wouldn't work. Any help is appreciated, thank you.
EDIT - So with the buttons I've made a little test to get the buttons from PlayerGui,
local DataStore = game:GetService("DataStoreService") local save = DataStore:GetDataStore("SavePos") local saveButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton local loadButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.OtherButton game.Players.PlayerAdded:Connect(function(plr) local spot = Instance.new("Folder",plr) spot.Name = "SaveLocation" local pos = Instance.new("CFrameValue",spot) pos.Name = "PlayerPosition" pos.Value = save:GetAsync(plr.UserId) or 0 saveButton.MouseButton1Click:Connect(function() end) loadButton.MouseButton1Click:Connect(function() end) end)
Now I want it to update pos.Value when saveButton is clicked on the HumanoidRootPart.CFrame, then maybe save could be used, not sure how really though, DataStores are not my strong suite. Then when you leave and come back, pressing loadButton would then load your position etc.
listen for when the button is clicked and fire to the server to save the position of the player. also the parent argument of Instance.new()
is deprecated, set the parent last.
local DataStore = game:GetService("DataStoreService") local save = DataStore:GetDataStore("SavePos") local rm = game.ReplicatedStorage.RemoteEvent -- mayb this was the remote event local function updateData(plr) -- update data local key = "id_"..plr.UserId pcall(function() save:UpdateAsync(key, function(old) return plr.SaveLocation.PlayerPosition.Value or old -- return the new value or the old one end) end) end game.Players.PlayerAdded:Connect(function(plr) local spot = Instance.new("Folder") local key = "id_"..plr.UserId spot.Name = "SaveLocation" spot.Parent = plr local pos = Instance.new("CFrameValue") pos.Name = "PlayerPosition" pos.Parent = spot local getPos = save:GetAsync(key) if getPos then local char = plr.Character or plr.CharacterAdded:Wait() pos.Value = getPos char:SetPrimaryPartCFrame(pos.Value) end local canSave = true rm.OnServerEvent:Connect(function(player, savePosition) -- listen for when the client clicks the save button if savePosition == "savePosition" and canSave then updateData(player) -- update the players pos print("saved!") canSave = false wait(10) -- to prevent it from updating the data too much which results in overloading the queue canSave = true end end) end)
client
local button = script.Parent -- define where the button is local rm = game.ReplicatedStorage.RemoteEvent button.Activated:Connect(function() rm:FireServer("savePosition") end)