I have been trying to save 2 values for a game that I'm working on, and I Can't get the script to work for some reason.
Here is the script -
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SufferingsStore = DataStoreService:GetDataStore("Sufferings")
local SufferCoinsStore = DataStoreService:GetDataStore("SufferCoins")
local function leaderboard(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local SufferCoins = Instance.new("IntValue")
SufferCoins.Name = "SufferCoins"
SufferCoins.Parent = leaderstats
local Sufferings = Instance.new("IntValue")
Sufferings.Name = "Sufferings"
Sufferings.Parent = leaderstats
local data1, data2
local success, errorMessage = pcall(function()
data1 = SufferCoinsStore:GetAsync(player.UserId.. "-SufferCoins")
data2 = SufferingsStore:GetAsync(player.UserId.. "-Sufferings")
end)
if success then
SufferCoins.Value = data1
Sufferings.Value = data2
print("Data succesfuly loaded for " .. player.Name)
else
warn(errorMessage)
end
end
local function saveData(player)
local success, errorMessage = pcall(function()
SufferCoinsStore:SetAsync(player.UserId.. "-SufferCoins", player.leaderstats.SufferCoins.Value)
SufferingsStore:SetAsync(player.UserId.. "-Sufferings", player.leaderstats.Sufferings.Value)
end)
if success then
print("Data Saved for " .. player.Name)
else
print("An Error Occurred While Saving for " .. player.Name)
warn(errorMessage)
end
end
Players.PlayerAdded:Connect(function(player)
leaderboard(player)
player.CharacterAdded:Connect(function()
leaderboard(player)
end)
end)
Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
for _, player in pairs(Players:GetPlayers()) do
leaderboard(player)
end
I tried watching Youtube Tutorials, asking ChatGPT(it was helpful once) and I hoped the Data would save. The script ended up not doing much, only printing out that it loaded / saved the data without actualy doing so.
Try with this and make sure that Roblox API Services are on, okey instead of doing a lot of functions for call the load and save data, you could use PlayerRemoving and PlayerAdded, and create your variables for the leaderstats, so maybe that's the problem, or you could use this code instead:
local player = game:GetService("Players")
local dataStore = game:GetService("DataStoreService")
local SufferingsDataStore = dataStore:GetDataStore("Sufferings")
local SufferCoinsDataStore = dataStore:GetDataStore("SufferCoins")
player.PlayerAdded:Connect(function(plr)
print(plr.Name .." joined")
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local Sufferings = Instance.new("IntValue", folder)
Sufferings.Name = "Sufferings"
Sufferings.Value = 0
local SufferCoins = Instance.new("IntValue", folder)
SufferCoins.Name = "SufferCoins"
SufferCoins.Value = 0
local infoSaved = nil
local infoSaved2 = nil
local succeful, fail = pcall(function()
infoSaved = SufferingsDataStore:GetAsync(plr.UserId)
infoSaved2 = SufferCoinsDataStore:GetAsync(plr.UserId)
end)
if succeful then
plr.leaderstats.Sufferings.Value = infoSaved
plr.leaderstats.SufferCoins.Value = infoSaved2
warn("Data loaded successfully")
end
end)
player.PlayerRemoving:Connect(function(plrE)
warn("Goodbye " ..plrE.Name.."!")
local points1 = plrE.leaderstats.SufferCoins.Value
local points2 = plrE.leaderstats.Sufferings.Value
local succeful, fail = pcall(function()
SufferingsDataStore:SetAsync(plrE.UserId, points2)
SufferCoinsDataStore:SetAsync(plrE.UserId, points1)
end)
if succeful then
warn("Data saved correctly")
end
end)
Create and order your variables, then copy and paste infoSaved many times as you want, then just set the infoSaved to the datastore get async inside the pcall, also set variables to infoSaved for load it plr.leaderstats.Sufferings.Value = infoSaved
when player removing do the same with points1
set points to plr.leaderstats.yourValue.value
, also set async data with points.