I want to make a shop GUI in my game. I have no problem with that. I know I have to use Remote Events to give the player items, but do I have to use multiple remote events for multiple items? Can't I just use one remote event or a few instead of having hundreds of them, each for a different item?
If this is possible then please tell me how can I do this because I have no idea whatsoever.
An example(if you can give) is appreciated.
The short answer is yes, you can have one remote event to handle purchases for all items
Easy. What you can do for your event listener, is add a parameter, which I will name request
. This will be what the client requests to buy.
local ServerStorage = game:GetService("ServerStorage") local items = ServerStorage.items -- a folder named items local purchaseEvent = ... -- path to your event local function handlePurchase(client, request) local tool = items:FindFirstChild(tostring(request)) end
What I am doing here, is calling :FindFirstChild()
just to make sure that the item they request is in the items
folder, just in case an exploiter tries to send bad information, and i call tostring
on request
as well, because again, exploiters.
local function handlePurchase(client, request) local tool = items:FindFirstChild(tostring(request)) if tool then if client.Cash.Value >= tool.price.Value then -- If the price is an int value or number value, in the tool. end end end purchaseEvent.OnServerEvent:Connect(handlePurchase)
Finally, from a LocalScript, in some TextButton:
local purchaseEvent = ... -- path to event script.Parent.Activated:Connect(function() purchaseEvent:FireServer(script.Parent.Name) end)
I send the name of the button to the server, which should be the name of the tool.