Hello, I am making a rebirth system for my obby game. The script is able to save the data, but I think its having a problem loading it back to the player when they rejoin. Can someone help me fix?
local players = game:GetService('Players')
local marketplace = game:GetService('MarketplaceService')
local datastoreservice = game:GetService("DataStoreService")
local myDataStore = datastoreservice:GetDataStore("myDataStore")
players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder', player)
leaderstats.Name = "leaderstats"
local Stages = Instance.new('IntValue', leaderstats)
Stages.Name = 'Stage'
local Rebirth = Instance.new('IntValue', leaderstats)
Rebirth.Name = 'Rebirths'
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-Rebirths")
data = myDataStore:GetAsync(player.UserId.."-Stage")
end)
if success then
Rebirth.Value = data
Stages.Value = data
print('loaded')
else
print("There was an error while getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-Rebirths", player.leaderstats.Rebirths.Value)
myDataStore:SetAsync(player.UserId.."-Stage",player.leaderstats.Stage.Value)
end)
if success then
print("player data has been saved")
else
print("There was an error")
warn(errormessage)
end
end)
I’m assuming the issue is that rebirths and stages load with the same value (the stage’s value)
and that’s because you’re overwriting the data value for rebirths in your script.
Remove this :
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-Rebirths")
data = myDataStore:GetAsync(player.UserId.."-Stage")
end)
if success then
Rebirth.Value = data
Stages.Value = data
print('loaded')
else
and replace it with :
local data
local success, errormessage = pcall(function()
data["Rebirths"] = myDataStore:GetAsync(player.UserId.."-Rebirths")
data["Stages"] = myDataStore:GetAsync(player.UserId.."-Stage")
end)
if success then
Rebirth.Value = data["Rebirths"]
Stages.Value = data["Stages"]
print('loaded')
else
Try putting local data as a table like in the script below
local data = {} -- Initialize the data table
local success, errormessage = pcall(function()
data["Rebirths"] = myDataStore:GetAsync(player.UserId.."-Rebirths")
data["Stages"] = myDataStore:GetAsync(player.UserId.."-Stage")
end)
if success then
Rebirth.Value = data["Rebirths"]
Stages.Value = data["Stages"]
print('loaded')
else