Values Not Updating in the Script

I’m encountering an issue where the values of workerLevel and machineLevel do not update properly in the script. Despite the values changing in the interface, the script always reads them as 1. This affects the calculation of income and money generation based on these levels. I’ve added debugging code to print their values, but they remain stuck at 1. How can I fix this issue and ensure the values are properly updated and recognized in the script? I’m using a server script

local prompt = script.Parent:FindFirstChildOfClass("ProximityPrompt")
local billboardgui = script.Parent.BillboardGui
local TextGive = billboardgui.TextCanGive

if prompt then
	prompt.Triggered:Connect(function(player)
		if not prompt.Enabled then return end
		prompt.Enabled = false

		local cash = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("cash")
		if not cash or cash.Value < 200 then
			warn("Not enough money to spawn a worker")
			return
		end
		cash.Value = cash.Value - 200

		local worker = game.ServerStorage.Worker:Clone()
		worker.Parent = workspace
		worker:SetPrimaryPartCFrame(prompt.Parent.PrimaryPart.CFrame * CFrame.new(0, 4.5, 0) * CFrame.Angles(0, math.rad(-90), 0))

		local leaderstats = player:FindFirstChild("leaderstatsvalue")
		if leaderstats then
			local cashCanGive = leaderstats:FindFirstChild("CashCanGive")
			local derevo = leaderstats:FindFirstChild("wood")
			local steel = leaderstats:FindFirstChild("steel")
			local workerLevel = leaderstats:FindFirstChild("workerLevel")
			local machineLevel = leaderstats:FindFirstChild("machineLevel")

			if not (cashCanGive and derevo and steel and workerLevel and machineLevel) then
				warn("Not enough data to calculate income")
				return
			end

			local function calculateIncome()
				local income = 10 * (1 + (workerLevel.Value - 1) * 0.2 + (machineLevel.Value - 1) * 0.3)
				TextGive.Text = "+$" .. math.floor(income)
				return income
			end

			while derevo.Value >= 2 and steel.Value >= 0.2 do
				print("workerLevel: " .. workerLevel.Value)  -- Откладка: вывод значения workerLevel
				print("machineLevel: " .. machineLevel.Value)  -- Откладка: вывод значения machineLevel
				local income = calculateIncome()
				cashCanGive.Value = cashCanGive.Value + income
				derevo.Value = derevo.Value - 2
				steel.Value = steel.Value - 1
				wait(5)
			end
		end
	end)
end

Any client UI that concerns data values doesn’t directly have influence over practically changing that value for the server. If a client changes the value by itself, that change is only relevant to UI or other clientside logic. The snippet you’ve provided correctly reads from the ValueObjects and isn’t the problem.

How is your game changing the “worker” or “machine” levels?

In the interface I have (conventionally), two buttons that I press to increase or decrease the values of worker and machine. As you understand, local scripts are used in the interface (UI)

Those LocalScripts need to use RemoteEvents to inform the server of value updates. The server must listen to these and can choose to apply those changes officially, so that snippet can dynamically react to what a client expects. You can also apply validation logic to ignore any extreme client inputs.

Thanks, I didn’t even think of that and was trying to solve the problem inside the script.

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