Need help with using RemoteEvents to change leaderstats

Hello,

I am trying to implement Developer Products into my game and want to use RemoteEvents to communicate to the server how much gold (my currency) to add to a certain player. There are a couple different products, each one that provides a different amount of gold for a different amount of robux.

The issue I am experiencing is that although the communication between Client and Server successfully works, the desired amount is never being added; the amount always remains at 0.

I have a picture of what the page looks like to a user.

Basically you choose from a DropDown menu that has fixed options to buy gold - 25, 50, 100, 250, etc. When you choose one of the options (all options are separate TextButtons), it changes two IntValues. One IntValue is edited to contain the value of the Developer Product that would be bought in the event that the user chooses to purchase. The other value is edited to have as much gold would be earned by the player in that same event. In the scripts down below, you can see how they are utilized.


I attached some snippets of my code down below.

This is a LocalScript I had attached to a button, which prompts the purchase.

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local AddGold = game.ReplicatedStorage.DevProduct_AddGold --RemoteEvent

script.Parent.Activated:Connect(function(player)
    local productID = script.Parent.Parent.DevProductID.Value --IntValue that holds the current selection's value
    local productInfo = MarketplaceService:GetProductInfo(productID, Enum.InfoType.Product)

    local player = Players.LocalPlayer
    MarketplaceService:PromptProductPurchase(player, productID)

    MarketplaceService.PromptProductPurchaseFinished:connect(function(userId, productId, isPurchased)
    	if isPurchased then
	 	    if productId == 998890745 then
			    AddGold:FireServer() --Should reward 25 gold to the player
			end
		end
	end)
end)

Now for my Script in ServerScriptStorage:

--Note that AddValue is an IntValue in StarterGui that represents how much gold should be given once something is purchased.
game.ReplicatedStorage.DevProduct_AddGold.OnServerEvent:Connect(function(player)
    local leaderstats = player.leaderstats
    local gold = leaderstats.Gold
	
	local addValue = game.StarterGui.MenuGUI.RobuxShopPage.MenuFrame.BuyGoldFrame.Frame.AddGold.Value
	
	print(addValue) --This prints, but the value always stays at 0
	
	gold.Value = gold.Value + addValue
end)

So far, I have tried many solutions. I tried adding the desired value as a parameter to FireServer(), which made it FireServer(25). It never worked, and I added Print statements throughout the code. Everything was executing properly and was going through, but the addValue.Value never changed.

Sorry for writing such a long description, wanted to provide as much information as possible. Anyone have any ideas as to how I could make this work? Any help would be appreciated.

1 Like

Hey! When you tried to get the AddValue, in

local addValue = game.StarterGui.MenuGUI.RobuxShopPage.MenuFrame.BuyGoldFrame.Frame.AddGold.Value

you are referencing the original StarterGUI which is set to 0. You would need to pass the gold amount through the remote event.

1 Like

First of all, let me start off by saying that your whole approach of this is super prone to hackers. You should never trust the client. What you should do is when the button is clicked, fire a remote event to the server to handle all of that.

--server
local function promptPurchase(player)
    marketPlaceService:PromptProductPurchase(player, productId)
end

local function onPromptFinished(player, assetId, purchased)
    if purchased then
        --give the currency
    end
end

--client
local function onClick()
    remoteEvent:FireServer() --the event that tells the server to prompt the player
end
2 Likes

I agree. You would need to access the PlayerGui to change it.

You can’t access the PlayerGui through the server since it’s client sided. You need to use a remote event.

Okay, thanks for the replies. When I pass information to the server, should I pass the desired value to be added, the Product ID, or both?

Yes and no. You shouldn’t tell the server how much it should add. Then hackers could just fire the remote with however much they want. I think it would be best to send the product id so the server knows what to prompt the client with.

1 Like

Okay it all makes sense now. I’ll go try out your approach right now.

1 Like