Issue with GUI text updating to number Values

I’m trying to make a crop planting script but I’m having trouble with the GUI.

Instead of the GUI updating according to each buttons number value it instead updates the text boxes of all buttons to the selected buttons number value.

Code:

-- Tool code
tool.Activated:Connect(function()
if CollectionService:HasTag(mouse.Target, "Tilled") then
		local cropSelect = cropSelect()
		local target = mouse.Target
		ReplicatedStorage.Remotes.CropPlacement:FireServer(target, seed, button)
		return

-- Placement code
rs.Remotes.CropPlacement.OnServerEvent:Connect(function(player, target, seed, button)
	if button.Count.Value <= 0 then return end
	print(button)
	button.Count.Value = button.Count.Value - 1
	local buttonValue = button.Count.Value
	rs.Remotes.ValueChange:FireClient(player, buttonValue)

-- GUI code
for buttonIndex, button in folder do

	button.Counter.Text = button.Count.Value
	game.ReplicatedStorage.Remotes.ValueChange.OnClientEvent:Connect(function(buttonValue)
		button.Counter.Text = buttonValue
	end)
2 Likes

This is the code that contains a bug:

for buttonIndex, button in folder do
	button.Counter.Text = button.Count.Value

	game.ReplicatedStorage.Remotes.ValueChange.OnClientEvent:Connect(function(buttonValue)
		button.Counter.Text = buttonValue
	end)
end

for each button inside of a folder, you are connecting an event and accepting a value, and then blindly assigning that value to the text property.
So when you fire the valuechanged event here say for example potatoes:

	rs.Remotes.ValueChange:FireClient(player, buttonValue)

it will fire the event for every crop, and every crop will receive that value, and assign it to their text property.
To fix this, the easiest way right now would be to just pass in the name or some other identifier of the target crop:

	rs.Remotes.ValueChange:FireClient(player, buttonValue, "Potatoes")

and then in the handler for the ui:

	game.ReplicatedStorage.Remotes.ValueChange.OnClientEvent:Connect(function(buttonValue, name)
		if name ~= button.Name then return end
        --assuming each button's name is the crop it is counting for.
        button.Counter.Text = buttonValue
	end)

and now only the correct crop should update.
Hope this helps!

1 Like

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