Trying to change player UI from server

Hello, I’m trying to make a currency system and I’m trying to make the currency handler edit the text from a players client and I’m not really sure how to do it.

It’s currently only on leaderstats only and I wanna switch it to a GUI.

local DataStoreService = game:GetService("DataStoreService")
local CurrencyStore = DataStoreService:GetDataStore("CurrencyStore")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
Players.PlayerAdded:Connect(function(Player)
	local UserId = Player.UserId
	local UserName = Player.Name
	local Currency = CurrencyStore:GetAsync(UserId)

	if Currency == nil then
		Currency = 0
		CurrencyStore:SetAsync(UserId, Currency)
	end

	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Coins = Instance.new("IntValue", Leaderstats)
	Coins.Name = "Coins"

	Coins.Value = Currency
	UserName.PlayerGui.ScreenGui.Coins.Text = Currency

	Coins.Changed:Connect(function(NewValue)
		CurrencyStore:SetAsync(UserId, NewValue)
		UserName.PlayerGui.ScreenGui.Coins.Text = NewValue
		
	end)
end)
1 Like

UserName represents a string [ the player’s name], thus, you can’t open the Ui from it.
Try this -

Player.PlayerGui.ScreenGui.Coins.Text = Currency

Same here -

UserName.PlayerGui.ScreenGui.Coins.Text = NewValue
Replace this with:
Player.PlayerGui.ScreenGui.Coins.Text = NewValue

To change UI from the server you should use a RemoteEvent. Use it to send the data to the player, and let the player update the UI locally.

On the server:

yourRemoteEvent:FireClient(player, NewValue)

On the client:

yourRemoteEvent.OnClientEvent:Connect(function(value)
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Coins.Text = value
)

Another option is to let the player itself detect the coins changing, that way you wouldn’t need to worry about it on the server. Somehting like this (local script):

local player =  game.Players.LocalPlayer
local coinValue = player:WaitForChild("leaderstats"):WaitForChild("Coins")
coinValue.Changed:Connect(function()
    player.PlayerGui.ScreenGui.Coins.Text = coinValue.Value
)