i am trying to save player data to data store using a script i found and modified The original script is from this website
I modified it so that it would store three values Experience as IntValue Rank as StringValue Bucks as IntValue
before i modified the script it worked but now it doesn't work anymore, i wonder have i referenced the data i want to store correctly?
my modified script is beloew
-- Data Storage
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local function onPlayerJoin(player) -- Runs when players join
local leaderstats = Instance.new("Folder") --Sets up leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local bucks = Instance.new("IntValue") --Sets up value for leaderstats
bucks.Name = "Bucks"
bucks.Parent = leaderstats
local exp = Instance.new("IntValue") --Sets up value for leaderstats
exp.Name = "Experience"
exp.Parent = leaderstats
local rank = Instance.new("StringValue") --Sets up value for leaderstats
rank.Name = "Rank"
rank.Parent = leaderstats
local playerUserId = "Player_" .. player.UserId --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
bucks.Value = data['Bucks']
exp.Value = data['Experience']
rank.Value = data['Rank']
else
-- Data store is working, but no current data for this player
bucks.Value = 50
exp.Value = 50
rank.Value = "Rec"
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

1 comment
On line 71, you are supposed to insert the value inside the table like this:. table.insert()