so i defined my leaderstats inside a folder that was called “leaderstats”. that folder was inside another folder called, “PlayerData”, and that folder was inside a serverscript in serverscriptservice.
i then made a different serverscript inside serverscriptservice and i asked for help. The dude fixed it, but then it broke and whenever i would add any values to the leaderstats it wouldn’t save. This is the script:
local DSS = game:GetService(“DataStoreService”)
local DataStore = DSS:GetDataStore(“LeaderstatsDataStoreTest1”)
local Players = game:GetService(“Players”)
game.Players.PlayerAdded:Connect(function(player)
local Coins = player:WaitForChild(“leaderstats”).Coins
local Gems = player:WaitForChild(“leaderstats”).Gems
local success, data = pcall(DataStore.GetAsync, DataStore, player.UserId)
if success then
Gems.Value = data.Gems
Coins.Value = data.Coins
print("Data loaded")
else
print("Error: ", data)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
Gems = player.leaderstats.Gems.Value,
Coins = player.leaderstats.Coins.Value
}
local success, error = pcall(DataStore.SetAsync, DataStore, player.UserId, data)
if success then
print("Data Successfully Saved!")
else
print("Error: ", error)
end
local DataStore = game:GetService("DataStoreService"):GetDataStore("MyData")
local data
local success, response = pcall(function()
data = DataStore:GetAsync("key")
end)
This is a full working demo. There are two parts in my Workspace, named GemsPart and CoinsPart and when the player steps on the parts, it increases their totals.
Put the following in ServerScriptService:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("LeaderstatsDataStoreTest")
game.Players.PlayerAdded:Connect(function(player: Player)
-- Create the leaderstats
local leaderStats = Instance.new("Folder")
leaderStats.Name = "leaderstats"
leaderStats.Parent = player
-- Create the Coins value
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderStats
-- Create the Gems value
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Value = 0
gems.Parent = leaderStats
-- Update this player's stats from DataStore
local success, data = pcall(DataStore.GetAsync, DataStore, player.UserId)
if success then
coins.Value = data.Coins
gems.Value = data.Gems
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
Gems = player.leaderstats.Gems.Value,
Coins = player.leaderstats.Coins.Value
}
local success, error = pcall(DataStore.SetAsync, DataStore, player.UserId, data)
end)
game.Workspace.GemsPart.Touched:Connect(function(other: BasePart)
local player = game.Players:GetPlayerFromCharacter(other.Parent)
if player then
player.leaderstats.Gems.Value += 1
end
end)
game.Workspace.CoinsPart.Touched:Connect(function(other: BasePart)
local player = game.Players:GetPlayerFromCharacter(other.Parent)
if player then
player.leaderstats.Coins.Value += 1
end
end)
I’m not sure what to tell you at this point. The above code works just fine for me. You can additionally add a call to BindToClose() but I don’t think that’s the core issue here.