I'm a complete beginner in Roblox scripting and not very knowledgeable on DataStoreService and leaderstats. If you look at my profile you'll see some posts from like a year ago, because I quit Roblox scripting and started scripting again recently.
When I mutate data in leaderstats it doesn't get saved, even though no errors and/or warnings appear in the console while I'm playing my game.
Here's the Script that uses DataStore in ServerScriptService. I followed this tutorial: https://www.youtube.com/watch?v=hWhr8ntzq5M
local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("PlayerData") local Players = game:GetService("Players") local function onPlayerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local coins = Instance.new("IntValue") coins.Name = "Coins" coins.Parent = leaderstats local multiplier = Instance.new("IntValue") multiplier.Name = "Multipliers" multiplier.Parent = leaderstats local playerUserId = "Player_" .. player.UserId local data = DataStore:GetAsync(playerUserId) print(data) if data then coins.Value = data["Coins"] multiplier.Value = data["Multipliers"] else coins.Value = 0 multiplier.Value = 1 end end local function create_table(player) local player_stats = {} for _, stat in pairs(player.leaderstats:GetChildren()) do player_stats[stat.Name] = stat.Value end return player_stats end local function onPlayerLeave(player) local player_stats = create_table(player) local success, errormessage = pcall(function() local playerUserId = "Player_" .. player.UserId DataStore:SetAsync(playerUserId, player_stats) end) if success then print("Data Saved") elseif not success then print("Could not save data") warn(errormessage) end end Players.PlayerAdded:Connect(onPlayerJoin) Players.PlayerRemoving:Connect(onPlayerLeave)
and the LocalScript that increases the coins in StarterPlayer/StarterCharacterScripts
local playerModel = script.Parent local Player = game.Players:GetPlayerFromCharacter(playerModel) local Mouse = Player:GetMouse() Player:WaitForChild("leaderstats") Player.leaderstats:WaitForChild("Coins") local coins = Player.leaderstats.Coins local multipliers = Player.leaderstats.Multipliers local function onLeftMouseButtonDown() coins.Value += 1 * multipliers.Value end Mouse.Button1Down:Connect(onLeftMouseButtonDown)
you cant increase from a local script.