Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to buy things with your in game currency?

Asked by
Tizzel40 243 Moderation Voter
6 years ago

I already made a shop Gui now I am trying to make sell a speed coil for 50 and my currency is called "cash". How do I make the item buyable and the when bought I get the item?

0
When you click the "buy" button, handle currency transfer (cash= cash - 50). Then clone the gear into the player's StarterPack. T0XN 276 — 6y

1 answer

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
6 years ago
Edited 6 years ago

Good day, Tizzel40.

I assume your game is filtering enabled, so you'd use InvokeServer and onServerInvoke to handle currency transfer.

Otherwise: Since you are looking at this and probably wondering why I assumed your game is filtering enabled, you can handle everything with the client. Should you use datastores, you MIGHT want to handle it with the server.

Enough with the tips, now. Let's get to the actual solution.

FOR FILTERING ENABLED: When a user presses the Buy button, the Client uses InvokeServer to send a request to the RemoteFunction. A serverscript recieves that request and gives the require item to the player. The Client does all the money checking, but the server will be the only one distributing the Speed Coil, because if the Client does, it likely wont be recognized by the server. Now, to put this into code form.

--I assume that the Speed Coil is in ReplicatedStorage.
replicatedStorage = game:GetService('ReplicatedStorage')
gameTools = replicatedStorage.gameTools
getBuyRequest = function(Player, ItemLocation)
    --*
    local Reciever = game:GetService('Players')[Player.Name]
    local itemToBuy = ItemLocation
    local newSpeedCoil = itemToBuy:Clone()
    newSpeedCoil.Parent = Reciever.StarterGear
    newSpeedCoil:Clone().Parent = Reciever.Backpack
end

replicatedStorage.remoteFunctions.handleBuying.OnServerInvoke = getBuyRequest

So then, the client can InvokeServer the remotefunction like so.

if game.Players.LocalPlayer.Money.Value>=tonumber(game.Players.LocalPlayer.PlayerGui.buySystem.buySpeedCoil.Cost.Text) then
    game:GetService('ReplicatedStorage').remoteFunctions.handleBuying:InvokeServer(game:GetService('ReplicatedStorage').gameTools.SpeedCoil)
else
    print('Too little money!')
end

Also, please note that you have to copy this script from the Copy Script button in the top right. I don't know if this only happens to me, but ScriptingHelpers cuts off line 1 by a lot.

FOR EXPERIMENTAL MODE: When a user presses the Buy button, the Client handles everything, unless you want to be fancy and still use remotefunctions in experimental mode. It distributes the Speed Coil to the LocalPlayer by itself. It distributes the gravity coil the same way the ServerScript does.

  • >> You might want to make an if statement to check if the ItemLocation is valid, so someone doesnt try to shove a banana into your RemoteFunction.

I hope this helped you with your problem. If you have any questions, don't be afraid to ask!

Ad

Answer this question