My Money goes into Negatives?

  1. I want to fix the leaderstats amount to stop going into negatives, because this affects the shops and my game’s leaderstats system can be abused.

Here is the script:

  • game.Players.PlayerAdded:Connect(function(player)
    
  • 	local a = Instance.new("Folder",player)
    
  • 	a.Name = "leaderstats"
    
  • 	local b = Instance.new("IntValue",a)
    
  • 	b.Name = "Pounds"
    
  • 	b.Value = 0
    
  • end)
    
1 Like

What is the full script? The snippet you’ve supplied is only it setting up the leaderstats.

Also, please properly format the code and don’t put it in bullet points.

Your issue is outside of that snippet, There’s code that changes the value to negative.

I’m not sure what is the problem, I cannot find the code for this, is there a keyword for this?

This is most likely happening because you are not checking the players cash value before they purchase something. You would need to check how much cash the player has and if it is more than or equal to the cost of the item in the shop. I can only presume this is the cause as you have not showed your script yet

1 Like

Do you have a system to buy items in place? If so, @topdog_alpha 's reply should be your solution.

When receiving a signal from remotevent “Buy”, On the server you need to run a check

if plr.Money.Value >= Item.Cost.Value then
--Buy
end
1 Like

Ah, yes, you are correct, I found the currency part.

elseif event == "Buy_Item" then
	local item = args[1]
	local player_pounds = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Pounds")
	
	if not player_pounds then warn("No pounds found for "..player.Name..", purchase failed") return end
	
	local player_tools = player_tool_data[player]
	local is_owned = player_tools[item.Name] --//See if the player already owns this tool
	
	if not is_owned then --//Prevent duplicate purchases
		player_pounds.Value = player_pounds.Value - item.Price.Value
		player_tools[item.Name] = true
		Give_Owned_Tools(player)
		return true, player_tools

Try this:

game.Players.PlayerAdded:Connect(function(player)
	local a = Instance.new("Folder",player)
	a.Name = "leaderstats"
	local b = Instance.new("IntValue",a)
	b.Name = "Pounds"
	b.Value = 0

b.Changed:Connect(function(NewValue)
b.Value = math.abs(NewValue)
end)
end)

It does not appear you are checking the player can actually afford to purchase the item. You are always allowing the player to purchase regardless of how much cash they have. You need to check the players cash before purchase

elseif event == "Buy_Item" then
	local item = args[1]
	local player_pounds = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Pounds")
	
	if not player_pounds or player_pounds.Value < item.Price.Value  then warn("Not enough pounds for "..player.Name..", purchase failed") return end
	

	local player_tools = player_tool_data[player]
	local is_owned = player_tools[item.Name] --//See if the player already owns this tool
	
	if not is_owned then --//Prevent duplicate purchases
		player_pounds.Value = player_pounds.Value - item.Price.Value
		player_tools[item.Name] = true
		Give_Owned_Tools(player)
		return true, player_tools

Make sure that the price value within the item exists for this line:

if not player_pounds or player_pounds.Value < item.Price.Value

This isn’t really a valid solution. This is a band aid over what is happening, but doesn’t fix the actual problem.

You are making the player buy the item without checking if they actually have enough to buy it, resulting in the player’s cash becoming a negative value.

Also @varjoy, that won’t work. If the player were to play an extremely expensive item but doesn’t have enough to buy it, they would gain a lot of money.

try this:

elseif event == "Buy_Item" then
	local item = args[1]
	local player_pounds = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Pounds")
	
	if not player_pounds or player_pounds.Value < item.Price.Value  then warn("Not enough pounds for "..player.Name..", purchase failed") return end
	

	local player_tools = player_tool_data[player]
	local is_owned = player_tools[item.Name] --//See if the player already owns this tool
	
	if not is_owned then --//Prevent duplicate purchases
        if player_pounds.Value >= item.Price.Value then 
		    player_pounds.Value = player_pounds.Value - item.Price.Value
		    player_tools[item.Name] = true
		    Give_Owned_Tools(player)
		    return true, player_tools
        end
2 Likes

Works beautiful! Thank you very much!

I would not recommend this solution. It does not prevent the purchase if the user does not have enough but it has the potential to lead to serious exploits where players could earn money simply by buying the most expensive item in the store.

For example: If I have 5 pounds and I purchase an item which costs 1,000 pounds then I will be left with 995 pounds.

local p = 5		-- p = +5
p = p - 1000	-- p = -995
p = math.abs(p)	-- p = +995