So im new to datastore, and the one i used before is very unreliable, so im trying to redo it, and I cannot figure out how, any help?
game.Players.PlayerAdded:Connect(function(player)
local DataStore = game:GetService("DataStoreService"):GetDataStore('taxzcxa')
local currentsave
local stats
local currentstat
local tabletosave
currentsave = DataStore:GetAsync(player.UserId)
stats = player:WaitForChild("stats")
local cash = player:FindFirstChild("stats"):FindFirstChild("Cash")
cash.Value = currentsave[cash]['Value']
local eye = player:FindFirstChild("stats"):FindFirstChild("EyeColor")
eye.Value = currentsave[eye]['Value']
end)
> local DoAutoSave = true
> local AutoSaveInterval = 120
>
> local DataStore = game:GetService("DataStoreService"):GetDataStore('taxzcxa')
>
> local currentsave
> local stats
> local currentstat
> local tabletosave
>
> game.Players.PlayerAdded:Connect(function(player)
> repeat wait() until player:FindFirstChild("stats")
> wait()
> currentsave = DataStore:GetAsync(player.UserId)
> stats = player:WaitForChild("stats")
> if currentsave then
> for i = 1,#currentsave do
> currentstat = stats:WaitForChild(currentsave[i]['Name'])
> currentstat.Value = currentsave[i]['Value']
> end
> print('Loaded stats')
> else
> print('Player is new and yes do be')
> end
> if DoAutoSave then
> spawn(function()
> repeat
> wait(AutoSaveInterval)
> if player then
> tabletosave = {}
> for _,v in pairs(player:WaitForChild("stats"):GetChildren()) do
> tabletosave[#tabletosave+1] = {Name = v.Name,Value = v.Value}
> end
> DataStore:SetAsync(player.UserId,tabletosave)
> else
> print('Player is not there, he dipped"')
> end
> until not player
> end)
> end
> end)
>
> game.Players.PlayerRemoving:Connect(function(player)
> if player then
> tabletosave = {}
> for _,v in pairs(player:FindFirstChild("stats"):GetChildren()) do
> tabletosave[#tabletosave+1] = {Name = v.Name,Value = v.Value}
> end
> DataStore:SetAsync(player.UserId,tabletosave)
> else
> print("The man is not there")
> end
> end)
>
> game.ReplicatedStorage.Save.OnServerEvent:Connect(function(player)
> if player then
> tabletosave = {}
> for _,v in pairs(player:WaitForChild("stats"):GetChildren()) do
> tabletosave[#tabletosave+1] = {Name = v.Name,Value = v.Value}
> end
> DataStore:SetAsync(player.UserId,tabletosave)
> print("did save")
> else
> print("The man is not there")
> end
> end)
local dss = game:GetService("DataStoreService")
local DataStore = dss:GetDataStore("taxzcxa")
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local stats = player:WaitForChild("stats")
local cash = stats.Cash
local eye = stats.EyeColor
local currentsave
local success, errormessage = pcall(function()
currentsave = DataStore:GetAsync(playerUserId)
end)
if currentsave then
if success then
cash.Value = currentsave.Cash
eye.Value = currentsave.Eye
end
end
local function saveData()
while wait(180) do
local currentsave = {
Cash = stats.Cash.Value
Eye = stats.EyeColor.Value
}
local s, m = pcall(function()
DataStore:SetAsync(currentsave)
end)
if s then
print("Successfully saved data")
else
print("There was an error while saving data")
warn(errormessage)
end
end
end
spawn(saveData)
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local stats = player:WaitForChild("stats")
local cash = stats.Cash
local eye = stats.EyeColor
local currentsave = {
Cash = stats.Cash.Value
Eye = stats.EyeColor.Value
}
local success, errormessage = pcall(function()
DataStore:SetAsync(currentsave)
end)
if success then
print("Successfully saved data")
else
print("There was an error while saving data")
warn(errormessage)
end
end)
It might have some errors though I did not test it because I am in a car and my battery is low so I want to send it before my laptop dies I can fix errors later. Tell me the errors if you have some.
I can explain the code later if you want.
Try playing the actual game. Sometimes the datastore does not work in studio for some reason. You can use the console to help change the leaderstats. The hotkey is f9 or you can type “/console”