What do you want to achieve? I want to make a Save LOcation system that fully works without any bug.
What is the issue? I had a save location script that was good, but sometimes it weren’t saving.
What solutions have you tried so far? I looked on the DevForum, youtube I found nothing.
If you want, I can provide you with the script which was working. But it weren’t saving every time.
local store = DataStore:GetDataStore("PlayerCharacterInfo") -- don't change the name of the DataStore or all data on it will be reset
local joined = false
game.Players.PlayerAdded:Connect(function(plr) --runs when the player joins
plr.CharacterAdded:Connect(function() --waits for the player character to be added to the game
if joined == false then
joined = true
--store:RemoveAsync(plr.UserId) --This code will Delete the last location just in case you want to reset a player's data for testing
local playerdata = store:GetAsync(plr.UserId)
if playerdata then --tests if the player joined the game before
plr.Character.HumanoidRootPart.CFrame = CFrame.new(playerdata[1], playerdata[2], playerdata[3]) --updats the player's Posistion if they joined before
end
wait(4)
while wait() and plr do --updates the play's Position every few seconds (5-15 seconds)
if plr then
local save = {}
table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.X)
table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.Y)
table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.Z)
store:SetAsync(plr.UserId,save)
end --if character loaded
end --while loop
end -- if they just joined
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
end)
first, use :PivotTo() not directly setting the cframe of the root part. thats how you teleport models
also, your part where you said “update the player every 5-15 seconds”. thats not every 5-15 seconds. thats every frame (or every time wait() ends) you are waiting 0 milliseconds which is basically waiting one frame. so you are updating the position so much that roblox is rate limiting you from updating the datastore
i also suggest you do one final save when the player is being removed (im not 100% sure if the character still exists, but you can check)
besides that, have you tried printing what gets returned from GetAsync, and printing what you save in the while loop (and if SetAsync doesnt error)
a suggestion unrelated to your problem:
use “task.wait()” not “wait()” its much faster
That’s how I made it, it doesnt work. How should I make it?
local DataStore = game:GetService("DataStoreService")
local store = DataStore:GetDataStore("PlayerCharacterInfo") -- don't change the name of the DataStore or all data on it will be reset
local joined = false
game.Players.PlayerAdded:Connect(function(plr) --runs when the player joins
plr.CharacterAdded:Connect(function() --waits for the player character to be added to the game
if joined == false then
joined = true
--store:RemoveAsync(plr.UserId) --This code will Delete the last location just in case you want to reset a player's data for testing
local playerdata = store:GetAsync(plr.UserId)
if playerdata then --tests if the player joined the game before
plr.Character.HumanoidRootPart:PivotTo(playerdata[1], playerdata[2], playerdata[3]) --updats the player's Posistion if they joined before
end
task.wait(4)
while task.wait() and plr do --updates the play's Position every few seconds (5-15 seconds)
if plr then
local save = {}
table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.X)
table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.Y)
table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.Z)
store:SetAsync(plr.UserId,save)
end --if character loaded
end --while loop
end -- if they just joined
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
end)
Before you test this, just run: game.DataStoreService:RemoveAsync(2203123161) in the command bar.
local DataStore = game:GetService("DataStoreService")
local store = DataStore:GetDataStore("PlayerCharacterInfo") -- don't change the name of the DataStore or all data on it will be reset
local positions = {}
local function WaitForRequestBudget(budgetType)
local request = pcall(function() return Enum.DataStoreRequestType[budgetType] end)
if not request return end
if DataStore:GetRequestBudgetForRequestType(request) <= 0 then return end
return true
end
local function save(player)
if not WaitForRequestBudget("UpdateAsync") then
repeat task.wait(3) until WaitForRequestBudget("UpdateAsync")
end
xpcall(function()
store:UpdateAsync(tostring(player.UserId), function() return positions[player] end)
end, function() warn(string.format("Error while saving %s's position!", player.Name) end)
positions[player] = nil
end
game.Players.PlayerAdded:Connect(function(player)
local position = pcall(function()
return store:GetAsync(tostring(player.UserId))
end)
if not position then return end
player.CharacterAdded:Once(function(character)
character:PivotTo(CFrame.new(position[1], position[2], position[3]))
character.HumanoidRootPart:GetPropertyChangedSignal("CFrame"):Connect(function()
positions[player] = {character:GetPivot()[1], character:GetPivot()[2], character:GetPivot()[3]}
end)
end)
end)
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(function()
for _, v in game.Players:GetPlayers() do
task.spawn(save, v)
end
task.wait(15)
end)
It works, I’ll test the game and if any bug occurs, I will update you. If it works perfectly without any hug, I will give you the solution. Also can you tell me a good way to find a save inventory script, without files that you put tools inside of it, just with a script?
Hello, here is the script. When 1 player is in game the location saves, but when there are 2 it doesn’t please help.
local store = DataStore:GetDataStore("PlayerCharacterInfo") -- don't change the name of the DataStore or all data on it will be reset
local joined = false
game.Players.PlayerAdded:Connect(function(plr) --runs when the player joins
plr.CharacterAdded:Connect(function() --waits for the player character to be added to the game
if joined == false then
joined = true
--store:RemoveAsync(plr.UserId) --This code will Delete the last location just in case you want to reset a player's data for testing
local playerdata = store:GetAsync(plr.UserId)
print(playerdata)
if playerdata then --tests if the player joined the game before
plr.Character.HumanoidRootPart:PivotTo(CFrame.new(playerdata[1], playerdata[2], playerdata[3])) --updats the player's Posistion if they joined before
end
task.wait(4)
while task.wait() and plr do --updates the play's Position every few seconds (5-15 seconds)
if plr then
local save = {}
table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.X)
table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.Y)
table.insert(save,plr.Character.HumanoidRootPart.CFrame.Position.Z)
store:SetAsync(plr.UserId,save)
end --if character loaded
end --while loop
end -- if they just joined
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
end)
Yes, it didn’t work. But why do I have to run the cmd that you sent? I want the script above fixed, because when there are 2 players it doesnt load you to the location that you were but if you are alone in the server it does load.