IF YOU HAVE FOUND THIS POST, THAT IS BECAUSE 6 DEVELOPERS HAVE TRIED AND FAILED TO FIX MY CODE! SO HERE I AM GOING TO GIVE YOU THE FULL RUNDOWN OF EVERYTHING GOING ON IN MY GAME!
THE PAGE:
THIS PAGE IS WHAT I AM STRUGGLING WITH!
(Scripted already!) For each of the settings, I have it so that whenever you click on yes and no, the text changes color, below is my code for each of them.
NO BUTTON! (Same code but different pathways to variables for each of them.)
local Players = game:GetService("Players")
local player = Players.LocalPlayer
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)
player.leaderstats.ForHire.Value = "No"
end)
YES BUTTON! (Same code but different pathways to variables for each of them.)
local Players = game:GetService("Players")
local player = Players.LocalPlayer
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)
player.leaderstats.ForHire.Value = "Yes"
end)
MY STRUGGLE:
I am trying to get my SaveData server script inside of ServerScriptService to save the data of 4 String Values but every developer who has tried hasn’t been able to get it correct. Can you help me out?
Below is my code.
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local runService = game:GetService("RunService")
local DataStore = DataStoreService:GetDataStore("UnofficialDataStorage")
-- 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 -- 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_" .. 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)
The output of the code above:
ON JOIN
(Toggled to show my issue.)
ON LEAVE
ON REJOIN
![Capture|690x57](upload://zsxMs94WZB7ZLsAuIoZajCmpP9i.png
I am offering a robux reward to the first person who can solve this issue. I will reward you with anywhere from 25-100 robux depending on how well you do and explain it to me.
Proof of robux:
IF THERE IS ANY OTHER INFORMATION YOU NEED, I WILL GIVE YOU IT!