WELCOME TO DEVLOG 2 OF ME CREATING DEVELOPER DREAMSCAPE!
MY ISSUE:
The script below saves the data in the leaderstats:
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local DataStore = DataStoreService:GetDataStore("UnofficialDataStorage")
local leaderstatValues = {
"ForHire",
"Hiring",
"Posting",
"Public"
}
for i, valueName in leaderstatValues do
local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = valueName
remoteEvent.Parent = ReplicatedStorage
remoteEvent.OnServerEvent:Connect(function(player, value)
if not value or type(value) ~= "boolean" then
return
end
player.leaderstats[valueName].Value = value and "Yes" or "No"
end)
end
-- Function to load player data
local function loadPlayerData(player)
local key = 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 -- error message only if failed, return value if it didn't
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.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"
for i, valueName in leaderstatValues do
local stringValue = Instance.new("StringValue")
stringValue.Name = valueName
stringValue.Value = "No"
stringValue.Parent = leaderstats
end
leaderstats.Parent = player
end
-- Connect the function to player added event
Players.PlayerAdded:Connect(function(player)
local playerData = loadPlayerData(player)
setupLeaderstats(player)
if playerData then
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 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)
And all of this works correctly. But next is with these pictures…
My Issue is that when the save script is fired, it saves solely in the leaderstats. And not in these values.
Here are my other scripts:
No ... - Updates First Photo And Second Photo Values
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("ForHire")
script.Parent.MouseButton1Click:Connect(function()
script.Parent.TextColor3 = Color3.fromRGB(255,67,67)
script.Parent.Parent.YesButton.TextColor3 = Color3.fromRGB(173, 173, 173)
script.Parent.Parent.Parent.Parent.Parent.Stats.ForHire.ForHireLabel.Text = "NO"
script.Parent.Parent.Parent.Parent.Parent.Stats.ForHire.ForHireLabel.TextColor3 = Color3.fromRGB(255,67,67)
RemoteEvent:FireServer(false)
end)
Yes ... - Updates First Photo And Second Photo Values
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("ForHire")
script.Parent.MouseButton1Click:Connect(function()
script.Parent.TextColor3 = Color3.fromRGB(85, 255, 127)
script.Parent.Parent.NoButton.TextColor3 = Color3.fromRGB(173, 173, 173)
script.Parent.Parent.Parent.Parent.Parent.Stats.ForHire.ForHireLabel.Text = "YES"
script.Parent.Parent.Parent.Parent.Parent.Stats.ForHire.ForHireLabel.TextColor3 = Color3.fromRGB(85,255,127)
RemoteEvent:FireServer(true)
end)
WHAT I WANT TO HAPPEN:
I want the leaderstats AND all of these values to save when the player joins, leaves, and rejoins.
VIDEO:
DEVLOG HISTORY:
- Devlog Help 1: Saving String Values - Look At Post.
SOLUTIONS LEADERBOARD:
1st: @FlipDip32 - 1 Post(s) Solved - Solved Post(s) 1
2nd TO BE DETERMINED
3rd TO BE DETERMINED
Created: 2 . 04. 2024 | 4:44 P.M. EST ()