Simple system doesn't work as intended

LocalScript:

local playerGui = game.Players.LocalPlayer.PlayerGui
local screenGui = playerGui.ScreenGui
local textLabel = screenGui.TextLabel
local intValue = screenGui.IntValue -- IntValue.Value is 0 by default.

local updateValueEvent = game:GetService("ReplicatedStorage").UpdateValueEvent -- Remote Event

local clickPart = workspace:WaitForChild("ClickPart")
local clickDetector = clickPart.ClickDetector

local function updateText()
	textLabel.Text = intValue.Value
end
clickDetector.MouseClick:Connect(function(playerWhoClicked)
	updateValueEvent:FireServer(playerWhoClicked)
	
	updateText()
end)

ServerScript:

local updateValueEvent = game:GetService("ReplicatedStorage").UpdateValueEvent

updateValueEvent.OnServerEvent:Connect(function(player)
	local intValue = player:WaitForChild("PlayerGui").ScreenGui.IntValue
	
	intValue.Value += 1
end)

The problem is that the Text of textLabel is set to 0, even when the Value of intValue is set to 1, and the Text is set to 1 when the Value is set to 2, and so on.

I want to make it so whenever the Value of intValue is equal to a specific number, the Text of textLabel is equal to the same Value, unfortunately the Text is just set to Value minus 1.

I tried using repeat task.wait() until intValue.Value > 0 but it still doesn’t work as intended.

Help is much appreciated!

1 Like

This is most likely happening because the updateText() function occurs before the remote’s connection is actually executed. It takes time for the remote to communicate to the server, so by the time the value is actually changed, the text already gets updated to the last value. You can fix the issue by using a property changed event like this:

local updateValueEvent = game:GetService("ReplicatedStorage").UpdateValueEvent -- Remote Event

local clickPart = workspace:WaitForChild("ClickPart")
local clickDetector = clickPart.ClickDetector

local function updateText()
	textLabel.Text = intValue.Value
end

intValue:GetPropertyChangedSignal("Value"):Connect(updateText) -- fires the function when the value is changed

clickDetector.MouseClick:Connect(function(playerWhoClicked)
	updateValueEvent:FireServer(playerWhoClicked)
end)

Hope this helps.

2 Likes

Thank you so much! I do appreciate it :slight_smile:

1 Like

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