A am trying to save a leaderstat called "coins" but it wont save and it makes two leaderstats one named "Coins" and one named "coins" with a lowercase c.
game.Players.PlayerAdded:Connect(function(plr) local stats = Instance.new("IntValue",plr) stats.Name = "leaderstats" local coins = Instance.new("IntValue",stats) coins.Name = "Coins" end) local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("CoinSaveSystem") game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("Folder",player) stats.Name = "leaderstats" local coins = Instance.new("IntValue",stats) coins.Name = "coins" coins.Value = ds:GetAsync(player.UserId) or 0 ds:SetAsync(player.UserId, coins.Value) coins.Changed:connect(function() ds:SetAsync(player.UserId, coins.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds:SetAsync(player.UserId, player.leaderstats.coins.Value) end)
local DataStoreService = game:GetService("DataStoreService") local Players = game:GetService("Players") local dataStore = game:GetService("DataStoreService"):GetDataStore("CoinStorage") local function autoSave() -- this is a much safer way of saving -- btw this checks if the player count is above 0 if #Players:GetPlayers() > 0 then for _, player in next, Players:GetPlayers() do dataStore:SetAsync(player.UserId, player:WaitForChild("leaderstats"):WaitForChild("Coins").Value) end end wait(120) -- go to roblox wiki and change it according to the times shown on that page autoSave() end -- wraps the call of autosave in a function so it doesn't go for more then twice amounts of time local recursiveCall = coroutine.wrap(autoSave) recursiveCall() -- run this event only once, not twice Players.PlayerAdded:Connect(function(player) local stats = Instance.new("Folder", player) stats.Name = "leaderstats" -- changed it to a number value so if you are using decimals the value can still be usable local coins = Instance.new("NumberValue", stats) coins.Name = "Coins" coins.Value = dataStore:GetAsync(player.UserId) or 0 --[[ I took out your changed event due to that being easily exploited to hackers. They can just change the value of their coins, and there it goes, it saves right there. Also if the "coins" value is changing rapidly, the connection to the data store wiil be throtlled at some point, and that's another reason why I took that out as well. --]] end) Players.PlayerRemoving:Connect(function(player) dataStore:SetAsync(player.UserId, player:WaitForChild("leaderstats"):WaitForChild("Coins").Value) end)
I suggest changing the name of the Data Store as well. If you have a second script for this, delete it. If you have more questions about it, make it one or comment on this.
The reason why the datastore isn't behaving like you were expecting is due to a typo on line 14, where you put coins.Name = "coins"
instead of coins.Name = "Coins"
. The script provided works perfectly on its own. However, it would appear that you have another script that is adding the value Coins to the leaderstats folder. This script is only monitoring coins, not Coins. So if you set Coins to 50, and leave coins at 0, the value saved will be the 0 stored in coins while Coins is ignored.
As for the problem of the other leaderstat; I can't replicate it from this script, suggesting that its a value generated from another script. (No, its not lines 1 - 6, while redundant, they don't cause the problem on their own) To fix that problem, check to see if any of your other scripts are creating new values in the leaderstats folder.