TextBox having weird behavior

I’m trying to make this text gui show the balance of the player using the following code:

Script
local TweenService = game:GetService("TweenService")

game.Players.PlayerAdded:Connect(function(player)
	local stats = game.ServerStorage:WaitForChild("PlayerMoney"):WaitForChild(player.Name)
	local gui = player.PlayerGui:WaitForChild("ScreenGui").Money
	local val = gui.Value
	val.Value = stats.Value
	
	local function makeTween(i)
		return TweenService:Create(val,
								   TweenInfo.new(.5, Enum.EasingStyle.Linear),
								   {Value = i}
		)
	end
	
	gui.Amount.Text = " $ "..tostring(val.Value).." "
	
	stats.Changed:Connect(function(i)
		makeTween(i):Play()
	end)
		
	val.Changed:Connect(function(i)
		gui.Amount.Text = " $ "..tostring(i).." "
	end)
end)

However, when I start up the game, the textbox only says TextBox. I have no idea why. When the player earns money and the stats.Changed and val.Changed events are called, there isn’t any weird behavior.

Part of the weirdness of the issues you’re encountering occurs from the fact that you’re trying to do what should be on the client-side on the server-side.

The server should have no business modifying Guis at all. The least it should do, if your money storage is in ServerStorage, is to call FireClient on a RemoteEvent per change to inform the client of their new balance. Everything else - from the tween, to the text setting and so on - should be done from the client.

2 Likes

Why can the server interact with the player’s GUI if that’s the case?

Product of standard replication behaviour. The server is responsible for populating PlayerGui with Guis from StarterGui on respawn (and repopulating on respawn if ResetOnSpawn is enabled), so Guis can be seen by the server as well.

The server’s only involvement should be, at the very least, this kind of replication. Guis should otherwise never be handled by the server and the client should be doing all interface work. Guis are intended to provide an interface for clients to modify and interact with to facilitate game features. The server is an interfaceless entity that should only be responsible for critical game actions (e.g. replication, validation, security, …).

2 Likes