I want to make a datastore script for my leaderboard stats.
Script doesn’t work, shows this error:
My script:
local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')
local function onPlayerJoin(player)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local miles = Instance.new('IntValue')
miles.Name = 'miles'
miles.Parent = leaderstats
local playerUserId = 'Player_'.. player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
miles.Value = data
else
miles.Value = 0
end
end
local function onPlayerExit(player)
local success, err = pcall(function()
local playerUserId = 'Player_'..player.UserId
playerData:SetAsync(playerUserId, player.leaderstats.miles.Value)
end)
if not success then
warn ('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)