Hello once again, i have a data store, but the problem is that it only saves data from a player who has never played before.
it saved any data they got but every time they join after it just loads the data they had the first time they left, ignoring anything they get in any joins after.
Script:
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player) --load data on join
local PlayerStatsKey = "PlayerSaveData-".. player.UserId
local StatsTemplate = script.Stats
local SoulsTemplate = script.SoulsOwned
local PlayerStatsFolder = Instance.new("Folder", player)
PlayerStatsFolder.Name = "PlayerStats"
local PlayerSoulsFolder = Instance.new("Folder", player)
PlayerSoulsFolder.Name = "OwnedSouls"
local PlayerStatsSave = PlayerDataStore:GetAsync(PlayerStatsKey)
print(PlayerStatsSave)
for _, Template in pairs(StatsTemplate:GetChildren()) do
local NewFolderStats = Instance.new(Template.ClassName, PlayerStatsFolder)
NewFolderStats.Name = Template.Name
NewFolderStats.Value = Template.Value
end
for _, Template in pairs(SoulsTemplate:GetChildren()) do
local NewFolderSouls = Instance.new(Template.ClassName, PlayerSoulsFolder)
NewFolderSouls.Name = Template.Name
NewFolderSouls.Value = Template.Value
end
if PlayerStatsSave then --if player is not a new player, load stats
for _, Save in pairs(PlayerStatsSave["PlayerStats"]) do
for _, PlayerStat in pairs(PlayerStatsFolder:GetChildren()) do
if PlayerStat.Name == Save[1] then
PlayerStat.Value = Save[2]
end
end
end
for _, Save in pairs(PlayerStatsSave["OwnedSouls"]) do
for _, PlayerStat in pairs(PlayerSoulsFolder:GetChildren()) do
if PlayerStat.Name == Save[1] then
PlayerStat.Value = Save[2]
end
end
end
else
print("wtf")
end
end)
game.Players.PlayerRemoving:Connect(function(player) --sava data on leave
local PlayerStatsKey = "PlayerSaveData-".. player.UserId
local PlayerStatsFolder = player.PlayerStats
local PlayerSoulsFolder = player.OwnedSouls
local PlayerStatsSave = {
PlayerStats = {
Strength = {"Strength", 1},
Defense = {"Defense", 1},
Speed = {"Speed", 1},
},
OwnedSouls = {
Soul1 = {"Soul1", false},
Soul2 = {"Soul2", false},
}
}
for _, Save in pairs(PlayerStatsSave["PlayerStats"]) do
for _, PlayerStat in pairs(PlayerStatsFolder:GetChildren()) do
if PlayerStat.Name == Save[1] then
Save[2] = PlayerStat.Value
end
end
end
for _, Save in pairs(PlayerStatsSave["OwnedSouls"]) do
for _, PlayerStat in pairs(PlayerSoulsFolder:GetChildren()) do
if PlayerStat.Name == Save[1] then
Save[2] = PlayerStat.Value
end
end
end
print(PlayerStatsSave)
PlayerDataStore:SetAsync(PlayerStatsKey, PlayerStatsSave)
end)
each folder has there respective stats
Example:
Player joins, gets 100 strength
player leaves, game says it saved 100 strength
player joins a second time, game loads 100 strength, player gets to 200 strength
player leaves, game says it saved 200 strength
player joins a third time, game only loaded 100 strength, and this happens every time after too