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

Do I have to use multiple remote events to give a player multiple items?

Asked by 5 years ago
Edited 5 years ago

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.

0
You really don't have to, One method I use is sending the command or purpose of the firing of the remote as an argument and varying what happens by checking that argument theking48989987 2147 — 5y
0
Sorry I don't understand what you mean. Can you explain it in a bit more detail? StrategicPlayZ 58 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Yes.

The short answer is yes, you can have one remote event to handle purchases for all items

How do you do it?

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.


Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments.
0
Thanks! StrategicPlayZ 58 — 5y
Ad

Answer this question