How would you make a shop like in a simulator game?

  1. What do you want to achieve? Keep it simple and clear!

I would like to know how you would create a shop like in a simulator game that has each item on a scrolling frame without having to make a script for each button.

  1. What is the issue? Include screenshots / videos if possible!

I don’t know if you should use an individual remote event for each button or a loop to check if a player interacted with one of the buttons.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried using remote events but it eventually just got too messy, with all the scripts everywhere.

I would like to know how you could do this.

1 Like

So, I watch a tutorial on how to make a shop from youtube video.
They use for loop to make all the button with just one remote event to buy all the stuff, also a remote function to get all the items info.
:sweat_smile:

That would work. I guess I didn’t research enough

1 Like

You can still reply to this thread with other ideas.

So basically you’d loop throught all of the buttons and just fire a remote event. I’m gonna give you an example.


-- vars --
 
local buttonsHolder = locationOfTheButton.ButtonsHolder
local buyRemote = game.ReplicatedStorage.BuyRemote

-- loop through the buttons --

for i, button in pairs(buttonsHolder:GetChildren()) do
   button.MouseButton1Click:Connect(function()
      if cash.Value >= price.Value then -- check if the player has enough cash to buy it, make sure you do this on the server as well
         remote:FireServer(itemYouAreBuying) -- there's quite a few ways to get the item, you could get it from the button's name or put in a StringValue inside the button and set it's value to the item's name.
      end
   end
end

Thanks, this is a better answer because it is actually explained with an answer.

1 Like

I have a question about this method though, wouldn’t this run only once? So it would run once and then it wouldn’t pick up any other times you click it.

Once it loops fully then it would stop.

Actually no, you only need to loop through the buttons once and that’ll do the job. Obviously if for some reason you add a new button you’d have to run the loop again, but that’s pretty simple, you’d just put the loop in a function and call it when you need it.

So would it loop whenever a button is pressed?

Oh ok, that makes sense thank you.

1 Like

No with the code I sent you it’ll loop only once and every button would work.

So it will work if a player presses any one of the buttons and you’ll be able to buy them?

Yes but I’ve only sent you the client script, you’d need to make a server one for the actual purchase of the item, if you want I could write you a server script example.

Yes could you write the example, but also for the sake of me not making another post how would I add a description to each item and the items stats? Because it would be random if it just said buy and you didn’t know how good it was or anything.

Sorry if im asking a lot of questions i’ve been scripting for a few months.

So for the server script…

-- vars --

local remote = game.ReplicatedStorage.BuyRemote

-- purchase code --

remote.OnServerEvent:Connect(function(plr, chosenItem) -- chosenItem being the item's name you sent on the client
    if cash.Value >= price.Value then -- now as for how you'd get the item's price is up to you, you can keep it in ServerStorage, ReplicatedStorage, inside the buy button, module script...
       item.Value += 1 -- adds 1 item 
    end
end

I don’t have much time but for the items stats, you could just put some values inside the button (NumberValue - for strength and such, StringValue - for description) and whenever you click or hover over the button you’d set some textlabel’s text to those values.

1 Like

Thanks for the answers, I’ll try what you said.

1 Like