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.
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.
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.
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.
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
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.
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.
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.
-- 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.