My Datastore works in game but there’s an issue where it only saves one person’s data rather than the rest, It only saves one person’s stats at a time only if they are in a server by themselves. If there’s anything wrong with it that doesn’t allow other players to save data, please let me know. Thanks.
The script is:
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("testing23")
local Holder = script.Stats
--//PlayerAdded
game.Players.PlayerAdded:Connect(function(players)
local Key = "PlayerID" .. players.userId
local GetSave = datastore:GetAsync(Key)
local DataFolder = Instance.new("Folder", players)
DataFolder.Name = "Stats"
local gold = Instance.new('IntValue') -- Gold
gold.Name = 'Gold'
gold.Parent = Holder
local statPoints = Instance.new('IntValue')
statPoints.Name = 'StatPoints'
statPoints.Parent = Holder
--//Level
local level = Instance.new('IntValue') -- Level
level.Name = 'Level'
level.Parent = Holder
local exp = Instance.new('IntValue') -- Exp
exp.Name = 'Exp'
exp.Parent = Holder
local nexp = Instance.new('IntValue') -- Nexp
nexp.Name = 'Nexp'
nexp.Parent = Holder
--//Stats
local vitality = Instance.new('IntValue') -- Vitality
vitality.Name = 'Vitality'
vitality.Parent = Holder
local defense = Instance.new('IntValue') -- Defense
defense.Name = 'Defense'
defense.Parent = Holder
local stamina = Instance.new('IntValue') -- Stamina
stamina.Name = 'Stamina'
stamina.Parent = Holder
local mana = Instance.new('IntValue') -- Mana
mana.Name = 'Mana'
mana.Parent = Holder
local power = Instance.new('IntValue') -- Power
power.Name = 'Power'
power.Parent = Holder
for i, Stats in pairs (Holder:GetChildren()) do
local Newstats = Instance.new(Stats.ClassName)
Newstats.Name = Stats.Name
Newstats.Value = Stats.Value
Newstats.Parent = DataFolder
end
if GetSave then
for i, Stats in pairs(DataFolder:GetChildren()) do
DataFolder[Stats.Name].Value = GetSave[i]
end
else
local SaveStats = {}
for i, Stats in pairs(DataFolder:GetChildren()) do
table.insert(SaveStats, Stats.Value)
end
datastore:SetAsync(Key, SaveStats)
end
while wait(10) do
local SaveStats = {}
for i, Stats in pairs(DataFolder:GetChildren()) do
table.insert(SaveStats, Stats.Value)
end
datastore:SetAsync(Key, SaveStats)
print('DataSaved')
end
game.Players.PlayerRemoving:Connect(function(ReturningPlayer)
local Key = "PlayerID" .. ReturningPlayer.userId
local SaveStats = {}
for i, Stats in pairs(DataFolder:GetChildren()) do
table.insert(SaveStats, Stats.Value)
end
datastore:SetAsync(Key, SaveStats)
print('DataSaved')
end)
end)