Code issues - Limits on spending money

Hello! I would like to know how to limit the money spent so that it is never negative, I want that if it is ‘<=10’, do not let money spend, how could i do that?

Script code:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

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 data
	
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		print("There was an error with getting your data.")
		wait(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
	end)
	
	if success then
		print("Player data successfully saved!")
	else
		print("There was an error when saving data.")
		warn(errormessage)
	end
end)

Spend script
– idk how to do that :frowning:

Could someone help me please? :frowning:

I know this is said a million times, but the scripting support forum is meant for help, not for people to create whole scripts.

Simply, wherever it is stored, you check the player’s money and if it is higher than the price, then you allow the purchase

  --  example:
    if money >= price then
    -- do stuff
    money -= price
    end
-- Saying  money -= price is the same as saying money = money - price
1 Like

thanks you bro! doing this would be fine, right?

-- Eggs types:
local legendary = script.Parent["Legendary blue"]
local epic = script.Parent["Epic red"]
local rare = script.Parent["Rare brown"]
local common = script.Parent["Common green"]

-- Eggs list:
local Eggs = {legendary = 5, epic = 15, rare = 30, common = 50}

-- Code
local Player = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent) 
	if PlayerCharacter then
		local Leaderstats = PlayerCharacter:WaitForChild("leaderstats")

		if Leaderstats.Cash.Value >= 10 then-- new

			Leaderstats.Cash.Value =  Leaderstats.Cash.Value - 10

			local Number = math.random(1,100)
			local chosen
			if Number <= Eggs.common then
				chosen = "common"
			elseif Number <= Eggs.rare + Eggs.common then
				chosen = 'rare'
			elseif Number <= Eggs.rare + Eggs.epic + Eggs.common then
				chosen = 'epic'
			else
				chosen = 'legendary'
			end
			print ("the random math value is: "..chosen)
		end -- end new
	end
end)