Cash check operations are not mathing?

I’ve create a pad for someone that allows for the user to click the pad and the player gets the item for a certain price.

Without the cash check (check if the player has enough to purchase it) it woks perfectly and subtracts the cash from the player, but with the check, if I have lets say 5000 cash, it goes to the else statement even though the price is 1500 and I have enough cash. I don’t know whats wrong. I’m so confused hered.

--Services.
local ServerStorage = game:GetService("ServerStorage")

--References.
local detector = script.Parent
local PurchaseFolder = ServerStorage.Weapons
local price = script.Parent.Parent.Price

--Handles Client To Server & Vise Versa Interactions.
detector.MouseClick:Connect(function(player)
	--Local variables not needed to be accessed from the outside of this interaction.
	local backpack = player.Backpack
	local item = PurchaseFolder:FindFirstChild("DB")
	local DataFolder = player:WaitForChild("DataFolder")
	local cash = DataFolder:WaitForChild("Currency")

	if cash.Value >= price.Value then
		--The process of giving the player the item.
		local itemClone = item:Clone()

		--Check if the player already has the specified item.
		if backpack:FindFirstChild("DB") then
			backpack:FindFirstChild("DB"):Destroy()
		end

		itemClone.Parent = backpack
		cash.Value = cash.Value - price
		print(cash.Value)
		print(backpack:GetDescendants())
	else
		-- You can add a message or handle the case where the player doesn't have enough cash.
		print("Not enough cash to purchase the item.")
	end
end)

You’re subtracting price, not price.Value.

Code:

--Services
local ServerStorage = game:GetService("ServerStorage")

--References
local PurchaseFolder = ServerStorage.Weapons
local detector = script.Parent
local price = detector.Parent.Price
print(price.Value)

--Handles Client To Server & Vise Versa Interactions
detector.MouseClick:Connect(function(player)
   --Local variables not needed to be accessed from the outside of this interaction
   local backpack = player.Backpack
   local DataFolder = player:WaitForChild("DataFolder")
   local cash = DataFolder:WaitForChild("Currency")
   local item = PurchaseFolder.DB

   if cash.Value >= price.Value then
   	--The process of giving the player the item
   	local itemClone = item:Clone()

   	--Check if the player already has the specified item
   	local currentItem = backpack:FindFirstChild("DB")
   	
   	if currentItem then
   		currentItem:Destroy()
   	end

   	itemClone.Parent = backpack
   	cash.Value -= price.Value
   	print(cash.Value)
   	print(backpack:GetDescendants())
   else
   	-- You can add a message or handle the case where the player doesn't have enough cash
   	print("Not enough cash to purchase the item")
   end
end)

Tell me what this prints.

In the original code, there is a .value at price.Value, it isnt there in the forum code for some reason.

Isnt that just what I did but simpler?
cash.Value = cash.Value - price.Value

Yeah, it’s the same. I only said to do that because it wasn’t in your original code.

Could you tell me what my script prints when you run the game though? I have a feeling your price value isn’t correct.

It prints “Not enough cash to purchase the item” which is what happens if the player has not enough. My friend got it to work though with a solution that makes no sense…
changing detector to script.Parent which is… shouldn’t fix the problem but I have a feeling what your solution was would fix it.

Thank you! :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.