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

Adding and Subtracting a value of money?

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

✓ Accepted answer

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

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