Calling out all scripters for help! - robux reward!

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
Capture
(Toggled to show my issue.)


ON LEAVE
Capture


ON REJOIN
Capture

![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:
Capture


IF THERE IS ANY OTHER INFORMATION YOU NEED, I WILL GIVE YOU IT!

2 Likes

When you change values on the client side, it doesn’t replicate to the server. This is where RemoteEvents come in.

Replace your server script with this:

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)

Then, in each button, make sure to fire the correct events.

For example, change your ForHire “No” code to this:

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)

And change your ForHire “Yes” code to this:

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)

Few tips for your future projects though:

  1. Minize the amount of scripts you’re using when you can, especially for your button code. With smart design, you could fit it all in one script and it would still look nice and readable
  2. Search things up if you don’t know how to achieve sometime, it’s harder to make 10 different posts than looking up for a solution yourself
3 Likes

Wow! It worked! Do you want your robux reward?

1 Like

No, this took me less than 10 minutes, it’s not needed.

4 Likes

alrighty then. Thanks for all the help!

2 Likes

6 developers and not 1 told you to use variables?

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)
1 Like

No problem. If you have any more problems, feel free to ask.

1 Like

apparently not :laughing: that’s actually funny.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.