Devlog Help 2: Showing String Values

gotchu, Ill get back to you soon.

uhmm:

Capture

and what do you mean: Stats?

what do I replace it with?

Each question leads to a targeted event, and you have to manually name them.

You change it to the frame that you showed on the second picture.

image

I couldnā€™t come up with a good name for the variable. Sorry about that.

So give me an example,

The first one is ā€œForHireā€

Cause likeā€¦

Capture

The Events table takes in an object as a key, and a RemoteEvent as the value.

So, ForHire would be:

local Events = {
	[questionFrame:FindFirstChild("ForHire")] = ReplicatedStorage:WaitForChild("ForHire")
}

first why so corny with the formats and excessive texts that isnt related, just get straight to the point

also:

game:BindToClose(function()
	if runService:IsStudio() then task.wait(3) return end

	for _, player in pairs(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)

No, I was talking about the OP.

I was just correcting part of the code.

Oh, I thought you were talking about me. My mistake.

Hereā€™s my code:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Leaderstats = Player:WaitForChild("leaderstats")

local Stats = script.Parent.Parent.Parent.Stats.Stats -- Replace this
local questionFrame = script.Parent -- Replace this as well

local Events = {
	[questionFrame:FindFirstChild("ForHire")] = ReplicatedStorage:WaitForChild("ForHire"),
	[questionFrame:FindFirstChild("Hiring")] = ReplicatedStorage:WaitForChild("Hiring"),
	[questionFrame:FindFirstChild("Posting")] = ReplicatedStorage:WaitForChild("Posting"),
	[questionFrame:FindFirstChild("Public")] = ReplicatedStorage:WaitForChild("Public")
}

local function formatQuestion(question, statsItem, value)
	local yesColor = Color3.fromRGB(85, 255, 127)
	local noColor = Color3.fromRGB(255, 67, 67)
	local greyColor = Color3.fromRGB(173, 173, 173)

	question.YesButton.TextColor3 = value and yesColor or greyColor
	question.NoButton.TextColor3 = value and noColor or greyColor

	statsItem.Label.Text = value and "YES" or "NO"
	statsItem.Label.TextColor3 = value and yesColor or noColor
end

for _, question in pairs(questionFrame:GetChildren()) do
	if not question:IsA("TextLabel") then continue end -- If the questions use a different object, replace this

	local RemoteEvent = Events[question]
	if not RemoteEvent then
		warn(`Event for {question.Name} does not exist.`)
		continue
	end

	local stat = Leaderstats:FindFirstChild(question.Name)
	if not stat then
		warn(`Stat for {question.Name} does not exist.`)
		continue
	end

	local statText = Stats:FindFirstChild(question.Name)

	formatQuestion(question, statText, stat.Value == "Yes")
	stat:GetPropertyChangedSignal("Value"):Once(function()
		formatQuestion(question, statText, stat.Value == "Yes")
	end)

	question.YesButton.MouseButton1Click:Connect(function()
		formatQuestion(question, statText, true)
		RemoteEvent:FireServer(true)
	end)

	question.NoButton.MouseButton1Click:Connect(function()
		formatQuestion(question, statText, false)
		RemoteEvent:FireServer(false)
	end)
end

And my error:

Maybe something you should know is that, with my other code, the remote events are created during the script.

leaderstats is not loading. ahdgfdyusijpd

You wanna review that code? Just seein

1 Like

Well here it is.

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)

Check your explorer, everything seems fine. A problem you may encounter is that you set the values after the leaderstats is parented to the player. Which maybe your problem, but this is for optimization and to make sure that some scripts donā€™t see datas as 0 when their not suppose to be.

Apparently the Events table contains nil as the key, in which the table errors. There might be an unloaded object inside, therefore it returns nothing. Maybe use WaitForChild instead.

Adding on to @kexy123, instead of using a table for your events, use a separate variable and what your doing on the script is:

local a = {[Instance1] = Instance1, [Instance2] = Instance2...}

Should be

local a1 = Instance1
local a2 = Instance2...

This doesnā€™t make sense at all.