RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"Script 'ServerScriptService.PlayerData', Line 43 - function setupPlayerData"
local DataStoreService = game:GetService("DataStoreService") local playerDataStore = DataStoreService:GetDataStore("PlayerData") local function setupPlayerData(player) local playerUserId = "Player_" .. player.UserId local data local success, err = pcall(function() data = playerDataStore:GetAsync(playerUserId) end) if success then if not data then data = { coins = 0, bag = 0 } playerDataStore:SetAsync(playerUserId, data) end else warn("Error while getting player data: " .. err) return end local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local coins = Instance.new("IntValue") coins.Name = "Coins" coins.Value = data.coins coins.Parent = leaderstats local bag = Instance.new("IntValue") bag.Name = "Bag" bag.Value = data.bag bag.Parent = leaderstats player.CharacterAdded:Connect(function(character) character:WaitForChild("Humanoid").Died:Connect(function() local success, err = pcall(function() playerDataStore:SetAsync(playerUserId, { coins = coins.Value, bag = bag.Value }) end) if not success then warn("Error while saving player data: " .. err) end end) end) end game.Players.PlayerAdded:Connect(setupPlayerData)