When a player leaves everyone else gets there datastore when they rejoin
local PlayerStatManager = {}
local KamiHandler = require(script.Parent.KamiHandler)
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local SaveDebounce = {}
local Data = {}
function PlayerStatManager:ChangeStat(player, statName, value, value2)
local playerUserId = "Player_" .. player.UserId
if typeof(Data[playerUserId][statName]) == "table" and value2 then
Data[playerUserId][statName][value] = value2
assert(typeof(Data[playerUserId][statName][value]) == typeof(value2), "ChangeStat error2: types do not match")
print("table value saved")
else
assert(typeof(Data[playerUserId][statName]) == typeof(value), "ChangeStat error1: types do not match")
Data[playerUserId][statName] = value
end
end
local function setupPlayerData(player)
print(player.Name.." <-- SETING UP")
local playerUserId = "Player_" .. player.UserId
local success, data = pcall(function()
return playerData:GetAsync(playerUserId)
end)
if success then
Data[playerUserId] = {
Level = 0,
Strength = 0,
}
if data then
for key, value in pairs(data) do
if typeof(Data[playerUserId][key]) ~= typeof(value) then
else
Data[playerUserId][key] = value
if typeof(Data[playerUserId][key]) == "table" then
for k, v in pairs(Data[playerUserId][key]) do
Data[playerUserId][key][k] = v
end
end
end
end
else
end
playerData:SetAsync(playerUserId, Data[playerUserId])
else
warn("Cannot access data store for player!")
end
end
local function savePlayerData(playerUserId)
if Data[playerUserId] then
local tries = 0
local success
repeat
tries = tries + 1
success = pcall(function()
playerData:SetAsync(playerUserId, Data[playerUserId])
print(playerUserId,Data[playerUserId]["height"])
end)
if not success then wait(1) end
until tries == 3 or success
if not success then
warn("Cannot save data for player!")
end
end
end
--Function to save player data on exit
local function saveOnExit(player)
local playerUserId = "Player_" .. player.UserId
savePlayerData(playerUserId)
end
game:BindToClose(function()
for _, player in ipairs(game.Players:GetPlayers()) do
print("Player_" .. player.UserId,player.Name.."aasdasdas")
savePlayerData("Player_" .. player.UserId)
end
end)
game.Players.PlayerAdded:Connect(setupPlayerData)
game.Players.PlayerRemoving:Connect(saveOnExit)
return PlayerStatManager
for some reason when a player rejoin it steals another person that joined at the same times datastore?