I’ve faced this issue a few days ago, but yes it happened again. I tried everything and as the title says, it is still the same. I don’t know what’s causing it, I need help! Here’s the script:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
leaderstats.Archivable = true
leaderstats.Parent = plr
local CatfoodValue = Instance.new("IntValue")
CatfoodValue.Name = "CatFood"
CatfoodValue.Archivable = true
CatfoodValue.Parent = leaderstats
local XPValue = Instance.new("IntValue")
XPValue.Name = "XP"
XPValue.Archivable = true
XPValue.Parent = leaderstats
local CoconutsValue = Instance.new("IntValue")
CoconutsValue.Name = "Coconuts"
CoconutsValue.Archivable = true
CoconutsValue.Parent = leaderstats
local SeashellsValue = Instance.new("IntValue")
SeashellsValue.Name = "Seashells"
SeashellsValue.Archivable = true
SeashellsValue.Parent = leaderstats
local Data
local PlayerID = "Player_"..plr.UserId
Data = DataStore:GetAsync(PlayerID)
if Data then
CatfoodValue.Value = Data.CatFood
XPValue.Value = Data.XP
CoconutsValue.Value = Data.Coconuts
SeashellsValue.Value = Data.Seashells
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Data = {
CatFood = plr.leaderstats.CatFood.Value,
XP = plr.leaderstats.XP.Value,
Coconuts = plr.leaderstats.Coconuts.Value,
Seashells = plr.leaderstats.Seashells.Value,
}
local PlayerID = "Player_"..plr.UserId
DataStore:SetAsync(PlayerID,Data)
end)
It is because you are not getting a new data store for every player.
local DataStore = DataStoreService:GetDataStore("DataStore")
-- instead use
local DataStore = DataStoreService:GetDataStore("Player_"..player.UserId)
-- so every player will have their own data stores.
And you might want to check out my module.
Edit:
I saw this and this is not true. You should warp them in a pcall function just like so.
success, err = pcall(function()
DataStore:SetAsync(PlayerID,Data)
end)
if success then
-- our data saved successfully
else
-- something wrong happened while saving data.
end