I want the following script which is attached to a button on a vending machine to charge the player X amount of cash (cash is my in game currency in (leaderstats) and deduct that amount from the player. Any help on this would be greatly appreciated.
function onClicked() local Bloxy = game.Lighting.DrinkandFood.DMLEnergy:clone() Bloxy.Parent = game.Workspace Bloxy.Handle.Position = script.Parent.Parent.HolderD.Position end script.Parent.ClickDetector.MouseClick:connect(onClicked)
player is the first parameter of MouseClick so you can use that to get the player. with the player, you can change their leaderstats, etc.
This code should work:
function onClicked(player) local Bloxy = game.Lighting.DrinkandFood.DMLEnergy:Clone() Bloxy.Parent = workspace Bloxy.Handle.Position = script.Parent.Parent.HolderD.Position local cost = 10 -- put your price in the cost variable local cash = player.leaderstats.cash if cash.Value >= cost then cash.Value = cash.Value - cost end if cash.Value < cost then return end end script.Parent.ClickDetector.MouseClick:Connect(onClicked)
If it doesn't work it could be something with the capitalization on stuff like 'cash' it could be a capital 'C'
If this is works for you make it the solution