So, I need an event to trigger whenever someone clicks on a button to buy an item. But, for some reason, the event isn't working! Help me please.
script that triggers the event:
Price = 3 Item = "Burger" be = game.workspace.Homes.Buy script.Parent.MouseButton1Click:Connect(function() local player = game.Players.LocalPlayer local Cash = player.leaderstats.Gold if Cash.Value >= Price then Cash.Value = Cash.Value - Price be:Fire(Item, player) end end)
script that holds the event:
RS = game:GetService("ReplicatedStorage") local function onEvent(Item, player) print("the event fires at least...") local item = RS.Item local itemC = item:Clone() itemC.Parent = player.Backpack print("WE DID IT!") end be = game.Workspace.Homes.Buy be.Event:Connect(onEvent)
First of all, you can only detect if a TextButton was clicked in a LocalScript. As soon as the client presses the Button, fire a RemoteEvent.
Now on a Script subtract the client's cash and add the item to the Backpack.
Notes:
Doing the purchase on the client can lead to an exploiter making the purchase for free.
Don't pass the price of the item in the RemoteEvent I'm pretty sure an exploiter can change the price.
There's no code because I don't want to create a shop right now since it can be time-consuming. I know this is not an answer but it should definitely help you.