How to send value to client

I want the server to send a value picked up from a numbervalue to the client but I keep getting errors and don’t know how to resolve it.

SERVER SIDE SCRIPT, is placed inside a screengui button called “Collect”

local Parent = script.Parent

local REP = game.ReplicatedStorage
local Available = Parent.Parent.NumberImage.IncomeDisplay.Available.Value -- Value that needs to be sent

Parent.MouseButton1Click:Connect(function()
	
	REP.MoneyTransfer:FireClient(Available)
	
	Available.Value = 0
	
	task.wait(2)
	
end)

CLIENT SIDE SCRIPT, is placed inside “StarterPlayerScripts”

local REP = game.ReplicatedStorage
local Player = game.Players.LocalPlayer

REP.MoneyTransfer.OnClientEvent:Connect(function(Available)
	
	Player.leaderstats.Aureus.Value += Available
	
	task.wait(1)
	
end)

CURRENT ERROR
image

2 Likes

change the remote event to a remote function, then on the client detect the click and invoke the server, then there you’ll get the value, but for future reference when you fire the client, the first value is the player to fire it to (and Available is not a player).

1 Like

When you fired the Client, you didn’t put in a player variable, so it didn’t send the value to anybody.
(Also I wouldn’t change the leaderstats value inside the local script as it would do nothing)

2 Likes

How would I get the player from the gui button?

1 Like

Hello!

In the first code block that you’ve mentioned, REP.MoneyTransfer:FireClient(Available) must be a player, you’re sending the “Available” value instead. It should be REP.MoneyTransfer:FireClient(Player, Available)

In order to get the player instance from the GUI button, you could change how your system works in a way where you detect the click from the client, fire a remote to the server, and do the variable change there as trusting the client(the user) with things such as in-game currency could be very risky because there wouldn’t be a way to validate the legitimacy.

client script under gui button

...
Parent.MouseButton1Click:Connect(function()
   Remote:FireServer(Available)
end)

server script

...
Remote.OnServerEvent:Connect(function(Player, Available)
   -- validate balance & edit leaderstats
   Player.leaderstats.Aureus.Value += Available
   NumberImage.IncomeDisplay.Available.Value = 0
end)

I hope this could help - remember, changing most things on the client won’t replicate to the server or to other players, meaning the server wouldn’t know the value of “Available”, and so the rest of the players.

2 Likes

I’ve tried doing this now but the local script does not detect when the “Available” value has changed and keep printing 0 when pressing the button and debug.

1 Like

Nvm, I manage to fix it was a small mistake

2 Likes

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