Leaderstats DataStore Issue

Hello!

I’m currently trying to make a leaderstats system that saves whenever the player leaves and joins back using DataStores, but can’t seem to make this script I followed on a tutorial work.

Help is much appreciated.


local Players = game:GetService("Players")
local leaderstats = Instance.new("Folder")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")

local database = DataStoreService:GetDataStore("data")
local sessionData = {}



leaderstats.Name = "leaderstats"

function PlayingAdded(player)	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "BitCoin"
	points.Value = 0


	local success = nil
	local playerData = nil
	local attempt = 1

	repeat
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)

		attempt += 1
		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5

	if success then
		print("Connected to date base")
		if not playerData then
			print("Assigning default data")
			playerData = {
				["points"] = 0,

			}
		end
		sessionData[player.UserId] = playerData
	else
		warn("Unable to get data for", player.UserId)
		player:Kick("We were Unable to load your data. You wouldn't want to start over would you? Didn't think so... Just go ahead and try again later")
	end

	points.Value = sessionData[player.UserId].points

	points.Changed:Connect(function()
		sessionData[player.UserId].points = points.Value
	end)

	points.Parent = leaderstats
end

Players.PlayerAdded:Connect(PlayingAdded)

function PlayerLeaving(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1

		repeat
			success, errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId])
			end)

			attempt += 1
			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempt == 5

		if success then
			print("Data saved for", player.Name)
		else
			warn("Unable to save for", player.Name)
		end

	end
end

Players.PlayerRemoving:Connect(PlayerLeaving)

function ServerShutdown()
	if RunService:IsStudio() then
		return
	end


	print("Server shutting down...")
	for i, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)
		end)
	end
end

game:BindToClose(ServerShutdown)
4 Likes
--> Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--> DataStore
local DataStore = DataStoreService:GetDataStore("User", "Data")

--> Data Template
local Template = {
    Bitcoin = 0,
}

--> Functions
local function getPlayerData(player: Player) --> Retrieve The Saved Data Of A Player, Or Create New Data
    local succ, data = pcall(function()
        return DataStore:GetAsync(tostring(player.UserId))
    end)

    if data == nil or (data ~= nil and typeof(data) ~= "table") then
        data = Template
    end

    return data
end

local function PlayerAdded(player: Player)
    local Data = getPlayerData(player)

    --> Create Leaderstats
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"

    local Bitcoin = Instance.new("NumberValue")
    Bitcoin.Name = "Bitcoin"

    Bitcoin.Value = Data.Bitcoin

    Bitcoin.Parent = leaderstats
    leaderstats.Parent = player
end

local function PlayerRemoving(player: Player)
    local leaderstats = player:FindFirstChild("leaderstats")
    local Bitcoin = leaderstats:FindFirstChild("Bitcoin")

    local DataTable = {
        Bitcoin = Bitcoin.Value,
    }

    pcall(function()
        DataStore:SetAsync(tostring(player.UserId), DataTable)
    end)
end

--> Race Condition (If a player loads in before this script runs)
for _, player: Player in pairs(Players:GetPlayers()) do
    PlayerAdded(player)
end

--> Connections
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

--> Shutdown
game:BindToClose(function()
    for _, player: Player in pairs(Players:GetPlayers()) do
        PlayerRemoving(player)
    end
end)
3 Likes

Can you explains more about your issue please ?

  • It can’t save data ?
  • It doesn’t change the value ?
  • Other ?
3 Likes

I’m not sure if it can save the data or not. It prints that it’s a success, which I’ve made it to where that string can only fire whenever the game saves the data. So I’m pretty sure it’s saving the data.

My only issue is that it doesn’t load the data, and I don’t know why.

2 Likes

Thank You for responding. I’m trying the code out now.

2 Likes

I ended up moving on from the project, and have came across the issue in another project of mine. I’m not giving up on this one, as I have gotten so much done to it. Help is needed… Please :frowning:

1 Like

Alright so, what’s the problem ?

1 Like