Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Adding and Subtracting a value of money?

Asked by 5 years ago

Hey so I am trying to get a functional shop working. As of now my MoneyData is stored in ReplicatedStorage and every player has a unique folder, this folder is the name of the player. I am wondering how can I take away/add money from the Value stored inside the unique player folder.

This is my current code for the shop purchase button

local Button = script.Parent
local Cost = 20
local DB = false

Button.MouseButton1Click:Connect(function(plr)
local key = plr.Name
local Money = game.ReplicatedStorage.MoneyData.key.Credits
if DB == false then

DB = true

    if Money.Value < Cost then

        error("Credits Too Low")

    elseif Money.Value >= Cost then

        print("Bought.")

        Money.Value = Player.Money.Value - Cost     
    end

wait(100)

DB = false

elseif DB == true then

    print("Cooldown Active")

end

end)

And this is where I specify the folder names:

wait()
local moneyData = Instance.new("Folder",game.ReplicatedStorage)
moneyData.Name = "MoneyData"

local ds = game:GetService("DataStoreService"):GetDataStore("MoneyDataStore")

game.Players.PlayerAdded:connect(function(plr)
    local plrMoney = Instance.new("Folder",moneyData)
    plrMoney.Name = plr.Name

    local key   = "id-"..plr.userId 

    local credits = Instance.new("IntValue",plrMoney)
    credits.Name = "Credits"
    credits.Value = 1000

1 answer

Log in to vote
0
Answered by
karlo_tr10 1233 Moderation Voter
5 years ago

Because you didn't give us any ouputs or more detail on what is not working I will fix some mistakes I spotted: Line 7, 1. script:

local Money = game.ReplicatedStorage.MoneyData[key].Credits

Line 20, 1. script:

Money.Value = Money.Value - Cost

Also at line 27, 1. script you can just type: else

Ad

Answer this question