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

How to make a text button reduce currency?

I am trying to make it so when a text button is clicked, it reduces currency from the player. Here is my currency script, which works just fine:

local DataStoreService = game:GetService("DataStoreService")
	local DefaultCash = 100
	local RETRY_LIMIT = 5--Retry.
	local myDataStore = DataStoreService:GetDataStore("myDataStore")
	local ErrorPlayers = {}--You need an error list for error players.
	
	game.Players.PlayerAdded:Connect(function(player)
	    local leaderstats = Instance.new("Folder")
	    leaderstats.Name = "leaderstats"
	    leaderstats.Parent = player
	 
	    local cash = Instance.new("IntValue")
	    cash.Name = "Cash"
	    cash.Parent = leaderstats
	    local Retries = 0
	    local data
	    local function TryFunction()
	        data = myDataStore:GetAsync(player.UserId.."-cash")
	    end
	    local success, errormessage = pcall(TryFunction)
	 
	    if success and data ~= nil then
	        cash.Value = data
	
	    elseif success == true then--First time
	        print('First time joining.')
	        cash.Value = DefaultCash
	
	    elseif success == false then--Error
	        print('An error occurred when trying to get ' .. player.Name .. "'s data, retrying...")
	        repeat
	        wait(0.5)
	        success, errormessage = pcall(TryFunction)
	        Retries = Retries + 1
	        print('Retrying ' .. Retries)
	        until Retries >= RETRY_LIMIT or success
	        if success then
	            cash.Value = data
		else
		
	            print("Player " .. player.Name .. " added to error players.")
	            ErrorPlayers[player.Name] = true
	        end
	    end
	end)
	 
	game.Players.PlayerRemoving:Connect(function(player)
	    if ErrorPlayers[player.Name] ~= true then
	        local success, errormessage = pcall(function()
	            myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
	        end)
	        if success then
	            print("Player Data Succesfully Saved!")
	
	        else
	            print("There Was An Error When Saving Data.")
	            warn(errormessage)
	        end
	    else--If the player is inside error
	        print("Not saving " .. player.Name .. "'s data because of the error list.")
	        ErrorPlayers[player.Name] = false--Remove from error list when leaving
	    end
	end)

But what I am having trouble with is this script:

cash = player..("leaderstats").Cash
	        if cash.Value >= 5 then
	           cash.Value = cash.Value - 5
	        else
	            print("Not enough Cash") 
		end
	end


Idk how to fix it so it takes away 5 cash when clicked but if you can help please do

1 comment

0

i dont know i i understood you right but you want to subtract 5 cash when the button is clicked. This script is exactly doing this

1 answer

YOURBUTTON.MouseButton1Click:Connect(function(player)
if player.leaderstats.Cash.Value >= 5 then
	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 5
else
	print("Not enough cash')

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