0

I am not entirely sure how to ask this question, but I want to use a variable when declaring a child's location in a hierarchy. Here is the code in ServerScriptService:

--omitted code
    game.Players.userName.leaderstats.Robux.Value += receiptInfo.CurrencySpent

-- Omitted code

userName is a global variable and not a child in a hierarchy. I want to use it to declare what child I am looking for.

In StarterPlayer.StarterPlayerScripts I have a local script which contains the global variable:

--omitted Code

local player = game.Players.LocalPlayer

--omitted code

userName = game.Players.LocalPlayer.Name

1 Answers1

1

Global variables are only defined within that script environment. _G table can be used to store variables and share them within all the localscripts. Same with modulescripts.

I'm assuming the server code you have is used to handle dev product transactions. What you should note is that upon purchase, the userid of the player who purchased is passed in the receiptinfo table. Here is an example:

local function processReceipt(receiptInfo)

    -- The line below gets the player instance by userid (as the name suggests)
    local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
    if player then
        -- What you want to when transaction was successfull
        return Enum.ProductPurchaseDecision.PurchaseGranted
    else
        game:GetService("ReplicatedStorage").PurchaseStatus:FireClient(player, false, nil, nil)
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end
end

game:GetService("MarketplaceService").ProcessReceipt = processReceipt

Let me know if you require any further assistance.

Cottient
  • 164
  • 8
  • I need a bit more assistance. I have been working on figuring out how to do it, and I reckon my best chance is with a module script. How would I share a variable between scripts in ServerScriptService and a local script in starter player scripts. Or how would I do it with _G tables. Is there a way to do this? – BusterCody3 Dec 24 '20 at 22:29
  • @BusterCody3 This is starting to sound like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you trying to achieve? You can use module scripts but something you should note of is that if the client makes changes to the modulescript, it will not replicate to the server. You can use RemoteEvents and RemoteFunctions if you want to parse data through the client and the server. – Cottient Dec 25 '20 at 02:59
  • I ended up using a remote event to solve this. – BusterCody3 Jan 05 '21 at 19:36
  • @BusterCody3 Sweet. Make sure to mark as answer if it helped :) – Cottient Jan 06 '21 at 10:14