Hello scripters.! This is my first devlog of me trying to create a developer game.
MY ISSUE:
When the value of the leaderstats change, they don’t save, they just return to “No”.
WHAT I WANT TO HAPPEN:
When the leaderstats change I want them to save in the leaderstats.
MY CURRENT SCRIPT:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DataStore = DataStoreService:GetDataStore("YourGameName")
-- Function to load player data
local function loadPlayerData(player)
local key = "Player_" .. player.UserId
local success, data = pcall(function()
return DataStore:GetAsync(key)
end)
if success then
return data
else
warn("Failed to load data for player " .. player.Name)
return nil
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 not success then
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
-- Set values from loaded data
player.leaderstats.ForHire.Value = playerData.ForHire
player.leaderstats.Hiring.Value = playerData.Hiring
player.leaderstats.Posting.Value = playerData.Posting
player.leaderstats.Public.Value = playerData.Public
end
end)
-- Connect the function to player removing event to save data
Players.PlayerRemoving:Connect(function(player)
local playerData = {
ForHire = player.leaderstats.ForHire.Value,
Hiring = player.leaderstats.Hiring.Value,
Posting = player.leaderstats.Posting.Value,
Public = player.leaderstats.Public.Value
}
savePlayerData(player, playerData)
end)
VIDEO:
DEVLOG HISTORY:
Devlog 1: Saving String Values - To Be Determined
SOLUTION’S LEADERBOARD:
1st: TO BE DETERMINED
2nd: TO BE DETERMINED
3rd: TO BE DETERMINED