I have a datastore that uses an already created Stats-folder instead of instancing one, but there is a problem: In singleplayer-mode it works just fine. No Problems. To see if it works multiplayer, I launched my server with two players, and only one of them actually gets a stats folder. How can I change my script, so that every player gets their own Stats?
My script:
dss = game:GetService("DataStoreService")
ds = dss:GetDataStore("Gast")
game.Players.PlayerAdded:Connect(function(plr)
local stats = game.ReplicatedStorage:FindFirstChild("Stats")
local gdata = game.ReplicatedStorage:FindFirstChild("GuiData")
if stats then
stats.Parent = plr
if gdata then gdata.Parent = plr
end
key = plr.UserId
stats = plr.Stats
gdata = plr.GuiData
local saves = {stats.Gems.Value,stats.Coins.Value,gdata.Greeting.Value,stats.Damage.Value,stats.Speed.Value,stats.Health.Value}
local data = ds:GetAsync(key,saves)
if data then
print("Data loaded for User "..plr.UserId.."!")
stats.Gems.Value = data[1]
stats.Coins.Value = data[2]
gdata.Greeting.Value = data[3]
stats.Damage.Value = data[4]
stats.Speed.Value = data[5]
stats.Health.Value = data[6]
else
print("No Data for "..plr.UserId.."! Setting to default.")
stats.Coins.Value = 0
stats.Gems.Value = 0
gdata.Greeting.Value = true
stats.Damage.Value = 0
stats.Speed.Value = 0
stats.Health.Value = 0
end
end
pcall(function()
game.Players.PlayerRemoving:Connect(function(plr)
key = plr.UserId
local stats = plr.Stats
local gdata = plr.GuiData
local saves = {stats.Gems.Value,stats.Coins.Value,gdata.Greeting.Value, stats.Damage.Value,stats.Speed.Value,stats.Health.Value}
ds:SetAsync(key,saves)
print("User "..plr.UserId.." left the game. Saved Data for User "..plr.UserId..".")
end)
end)
As always, thanks in advance!

3 comments
How can a datastore only save for one player? it makes no sense.
I don't know
ohmygod pls use variables for gems value and other stuff which you use commonly.It is very confusing