Script isn't saving player data to data store, have i referenced it correctly? [closed]
Asked by
4 years ago Edited 4 years ago
hi can anyone help figure out why this isnt saving the data?
002 | local DataStoreService = game:GetService( "DataStoreService" ) |
004 | local playerData = DataStoreService:GetDataStore( "PlayerData" ) |
008 | local function onPlayerJoin(player) |
010 | local leaderstats = Instance.new( "Folder" ) |
012 | leaderstats.Name = "leaderstats" |
014 | leaderstats.Parent = player |
018 | local bucks = Instance.new( "IntValue" ) |
022 | bucks.Parent = leaderstats |
026 | local exp = Instance.new( "IntValue" ) |
028 | exp.Name = "Experience" |
030 | exp.Parent = leaderstats |
033 | local rank = Instance.new( "StringValue" ) |
037 | rank.Parent = leaderstats |
041 | local playerUserId = "Player_" .. player.UserId |
043 | local data = playerData:GetAsync(playerUserId) |
047 | bucks.Value = data [ 'Bucks' ] |
049 | exp.Value = data [ 'Experience' ] |
051 | rank.Value = data [ 'Rank' ] |
069 | local function create_table(player) |
071 | local player_stats = { } |
073 | for _, stat in pairs (player.leaderstats:GetChildren()) do |
075 | player_stats [ stat.Name ] = stat.Value |
085 | local function onPlayerExit(player) |
089 | local player_stats = create_table(player) |
091 | local success, err = pcall ( function () |
093 | local playerUserId = "Player_" .. player.UserId |
095 | playerData:SetAsync(playerUserId, player_stats) |
097 | print ( "Player Data Stored" ) |
105 | warn( 'Could not save data!' ) |
113 | game.Players.PlayerAdded:Connect(onPlayerJoin) |
115 | game.Players.PlayerRemoving:Connect(onPlayerExit) |