Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make this script charge the player ("cash") from leaderstats?


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) 

1 comment

0

I appreciate the response. Ive tried a few variations and still cant get it. Looking for someone to show me.

2 answers

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

2 comments

0

well..looks good. Maybe im missing something though. The end of the (then) statements. Im still getting a error "attempt to compare string <= number " Line 9 onClicked

0

@shutemupschuetz My bad, you see the 'cost' variable. Just replace it without quotation marks. Instead of "10" replace it with 10. I also changed the script too.

This discussion is preserved as it was. New answers, comments, and votes are closed.