Does anyone know how to fix my code?

HELP IM TRYING TO SAVE SOME STRING VALUES!

local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local runService = game:GetService("RunService")

local DataStore = DataStoreService:GetDataStore("ABCDEF")

-- Function to load player data
local function loadPlayerData(player)
    local key = "Player_" .. player.UserId

    local success, errorMessage = pcall(function()
        return DataStore:GetAsync(key)
    end)

    if success then --no errors
        print("Player data loaded successfully for", player.Name)
    elseif not success and errorMessage then
        warn("Failed to load data for player " .. player.Name .. ": " .. errorMessage)
    end
end

-- Function to save player data
local function savePlayerData(player, data)
    local key = "Player_" .. player.UserId

    local success, errorMessage = pcall(function()
        DataStore:SetAsync(key, data)
    end)

    if success then
        print("Player data saved successfully for", player.Name)
    else
        warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage)
    end
end

-- Create leaderstats and set initial values
local function setupLeaderstats(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"

    local forHire = Instance.new("StringValue")
    forHire.Name = "ForHire"
    forHire.Value = "No"
    forHire.Parent = leaderstats

    local hiring = Instance.new("StringValue")
    hiring.Name = "Hiring"
    hiring.Value = "No"
    hiring.Parent = leaderstats

    local posting = Instance.new("StringValue")
    posting.Name = "Posting"
    posting.Value = "No"
    posting.Parent = leaderstats

    local public = Instance.new("StringValue")
    public.Name = "Public"
    public.Value = "No"
    public.Parent = leaderstats

    leaderstats.Parent = player
end

-- Connect the function to player added event
Players.PlayerAdded:Connect(function(player)
    local playerData = loadPlayerData(player)

    if not playerData then
        setupLeaderstats(player)
    else
        player:WaitForChild("leaderstats").ForHire.Value = playerData.ForHire;
        player:WaitForChild("leaderstats").Hiring.Value = playerData.Hiring;
        player:WaitForChild("leaderstats").Posting.Value = playerData.Posting;
        player:WaitForChild("leaderstats").Public.Value = playerData.Public;
    end
end)

Players.PlayerRemoving:Connect(function(player)
    local playerData = {
        ForHire = player:WaitForChild("leaderstats").ForHire.Value,
        Hiring = player:WaitForChild("leaderstats").Hiring.Value,
        Posting = player:WaitForChild("leaderstats").Posting.Value,
        Public = player:WaitForChild("leaderstats").Public.Value
    }

    savePlayerData(player, playerData)
end)

game:BindToClose(function()
    if runService:IsStudio() then task.wait(3) return end
    for _, player in pairs(game.Players:GetPlayers()) do
        local playerData = {
            ForHire = player:WaitForChild("leaderstats").ForHire.Value,
            Hiring = player:WaitForChild("leaderstats").Hiring.Value,
            Posting = player:WaitForChild("leaderstats").Posting.Value,
            Public = player:WaitForChild("leaderstats").Public.Value
        }

        savePlayerData(player, playerData)
    end
    task.wait(3)
    print("Server shutting down. Player data saved.")
end)

Can anyone help?

3 Likes

Your loadPlayerData function doesn’t return any data.

local function loadPlayerData(player)
    local key = "Player_" .. player.UserId

    local success, errorMessage = pcall(function()
        return DataStore:GetAsync(key)
    end)

    if success then --no errors
        return errorMessage -- error message only if failed, return value if it didn't
        print("Player data loaded successfully for", player.Name)
    elseif not success and errorMessage then
        warn("Failed to load data for player " .. player.Name .. ": " .. errorMessage)
    end
end

thats not supposed to happen…

Just move the print statement above it, code stops executing after a function returns

just did that… lets see if your correct!

uhmmm

Nothing shows up in the leaderstats.

local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local runService = game:GetService("RunService")

local DataStore = DataStoreService:GetDataStore("ABCDEF")

-- Function to load player data
local function loadPlayerData(player)
	local key = "Player_" .. player.UserId

	local success, errorMessage = pcall(function()
		return DataStore:GetAsync(key)
	end)

	if success then --no errors
		print("Player data loaded successfully for", player.Name)
		return errorMessage
	elseif not success and errorMessage then
		warn("Failed to load data for player " .. player.Name .. ": " .. errorMessage)
	end
end

-- Function to save player data
local function savePlayerData(player, data)
	local key = "Player_" .. player.UserId

	local success, errorMessage = pcall(function()
		DataStore:SetAsync(key, data)
	end)

	if success then
		print("Player data saved successfully for", player.Name)
	else
		warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage)
	end
end

-- Create leaderstats and set initial values
local function setupLeaderstats(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local forHire = Instance.new("StringValue")
	forHire.Name = "ForHire"
	forHire.Value = "No"
	forHire.Parent = leaderstats

	local hiring = Instance.new("StringValue")
	hiring.Name = "Hiring"
	hiring.Value = "No"
	hiring.Parent = leaderstats

	local posting = Instance.new("StringValue")
	posting.Name = "Posting"
	posting.Value = "No"
	posting.Parent = leaderstats

	local public = Instance.new("StringValue")
	public.Name = "Public"
	public.Value = "No"
	public.Parent = leaderstats

	leaderstats.Parent = player
end

local function create_stats(leaderstats)
	local forHire = Instance.new("StringValue")
	forHire.Name = "ForHire"
	forHire.Parent = leaderstats

	local hiring = Instance.new("StringValue")
	hiring.Name = "Hiring"
	hiring.Parent = leaderstats

	local posting = Instance.new("StringValue")
	posting.Name = "Posting"
	posting.Parent = leaderstats

	local public = Instance.new("StringValue")
	public.Name = "Public"
	public.Parent = leaderstats
end

-- Connect the function to player added event
Players.PlayerAdded:Connect(function(player)
	local playerData = loadPlayerData(player)
	
	if not playerData then
		setupLeaderstats(player)
	else
		
		local leaderstats = Instance.new("Folder",player)
		leaderstats.Name = "leaderstats"
		
		create_stats(leaderstats)
		
		leaderstats.ForHire.Value = playerData.ForHire;
		leaderstats.Hiring.Value = playerData.Hiring;
		leaderstats.Posting.Value = playerData.Posting;
		leaderstats.Public.Value = playerData.Public;
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local playerData = {
		ForHire = player:WaitForChild("leaderstats").ForHire.Value,
		Hiring = player:WaitForChild("leaderstats").Hiring.Value,
		Posting = player:WaitForChild("leaderstats").Posting.Value,
		Public = player:WaitForChild("leaderstats").Public.Value
	}

	savePlayerData(player, playerData)
end)

game:BindToClose(function()
	if runService:IsStudio() then task.wait(3) return end
	for _, player in pairs(game.Players:GetPlayers()) do
		local playerData = {
			ForHire = player:WaitForChild("leaderstats").ForHire.Value,
			Hiring = player:WaitForChild("leaderstats").Hiring.Value,
			Posting = player:WaitForChild("leaderstats").Posting.Value,
			Public = player:WaitForChild("leaderstats").Public.Value
		}

		savePlayerData(player, playerData)
	end
	task.wait(3)
	print("Server shutting down. Player data saved.")
end)

here’s a fixed version of your script.
the main reason as to why it wasn’t working was because of there being no presence of the “leaderstats” folder when the data was loaded and also the fact that no data was being returned from “loadPlayerData”.

i’ve made it so that the script will create a “leaderstats” folder when the player loads into the game with the data.
i’ve also made a “create_stats” function into your script which will be called once the script loads the player’s data from DataStoreService. this will create the values which will then display in the leaderboard.

and as for the “loadPlayerData” function i’ve added a line which will return the data back.

hopefully this works!

Hey! Thanks for the response, but this was already figured out. Go check out this post right here and solve this one aswell if you’d like: Devlog Help 2: Showing String Values