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:
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 = infoSavedwhen player removing do the same withpoints1set points toplr.leaderstats.yourValue.value, also set async data with points.