Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Multiple Data Stores
- -- Turn on Studio API Services if you haven't
- local dataStore = game:GetService("DataStoreService")
- local playerData = dataStore:GetDataStore("playerData")
- -- Some functions
- local function onPlayerJoin(player)
- local stats = Instance.new("Folder", player)
- stats.Name = "leaderstats"
- local cash = Instance.new("IntValue", stats)
- cash.Name = "Cash"
- local gold = Instance.new("IntValue", stats)
- gold.Name = "Gold"
- local playerUserId = "Player_"..player.UserId
- local data = playerData:GetAsync(playerUserId)
- if data then
- cash.Value = data["Cash"]
- gold.Value = data["Gold"]
- else
- cash.Value = 10 -- Default cash
- gold.Value = 10 -- Default gold
- end
- end
- local function create_table(player)
- local player_stats = {}
- for i, stat in pairs(player.leaderstats:GetChildren()) do
- player_stats[stat.Name] = stat.Value
- end
- return player_stats
- end
- local function onPlayerExit(player)
- local player_stats = create_table(player)
- local playerUserId = "Player_"..player.UserId
- local success, err = pcall(function()
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement