local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local playerDatatwo = DataStoreService:GetDataStore("PlayerData") local function onPlayerJoin(player) local UserFolder = Instance.new("Folder") UserFolder.Name = "UserFolder" UserFolder.Parent = player local gold = Instance.new("IntValue") gold.Name = "Gold" gold.Parent = UserFolder local petvalue = Instance.new("IntValue") petvalue.Name = "petvalue" petvalue.Parent = UserFolder local playerUserId = "Player_" .. player.UserId local data = playerData:GetAsync(playerUserId) local datatwo = playerDatatwo:GetAsync(playerUserId) if datatwo then petvalue.Value = datatwo else petvalue.Value = 0 end if data then gold.Value = data else gold.Value = 0 end end local function onPlayerExit(player) --Runs when players exit local success, err = pcall(function() local playerUserId = "Player_" .. player.UserId playerDatatwo:SetAsync(playerUserId, player.UserFolder.Petvalue.Value) playerData:SetAsync(playerUserId, player.UserFolder.Gold.Value) end) if not success then warn('Could not save data!') end end game.Players.PlayerAdded:Connect(onPlayerJoin) game.Players.PlayerRemoving:Connect(onPlayerExit)
Lucky, I found out how to do this
local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local function onPlayerJoin(player) local UserFolder = Instance.new("Folder") UserFolder.Name = "UserFolder" UserFolder.Parent = player local Coin = Instance.new("IntValue") Coin.Name = "Coins" Coin.Parent = UserFolder local Gem = Instance.new("IntValue") Gem.Name = "Gems" Gem.Parent = UserFolder local playerUserId = "Player_" .. player.UserId local data = playerData:GetAsync(playerUserId) if data then Coin.Value = data['Coins'] Gem.Value = data['Gems'] else Coin.Value = 0 Gem.Value = 0 end end local function create_table(player) local player_stats = {} for _, stat in pairs(player.UserFolder:GetChildren()) do player_stats[stat.Name] = stat.Value end return player_stats end local function onPlayerExit(player) local player_stats = create_table(player) local success, err = pcall(function() local playerUserId = "Player_" .. player.UserId playerData:SetAsync(playerUserId, player_stats) end) if not success then warn('Could not save data!') end end game.Players.PlayerAdded:Connect(onPlayerJoin) game.Players.PlayerRemoving:Connect(onPlayerExit)