Purchase ability sometimes ends up with negative numbers

  1. What do you want to achieve?
    I want the player to be able to purchase a texture/skin by selecting a button at the top of their screen, deducting currency if they have the required amount.

  2. What is the issue?
    For some odd reason, this ends up with negative numbers only sometimes. Usually about every 1/10 times.

  3. What solutions have you tried so far?
    I have simplified the client script, such as removing if texture ~= floorTexture.Texture then, but this is important to the script and does not fix the issue anyways.

Client Script (Handles the selection of the button, checks if the player has required points, Makes sure the player is not receiving the same texture/skin, applies texture/skin):

local player = game.Players.LocalPlayer
local floorTexture = workspace.Area.Platform.FloorTexture
local buyEvent = game.ReplicatedStorage.BuyFloorEvent

local whiteBlackCheckers = "rbxassetid://6625549840"
local redBlackCheckers = "rbxassetid://6625569786"
local polkaDots = "rbxassetid://6625160551"
local woodPlanks = "rbxassetid://6657105968"
local grass = "rbxassetid://6661754477"

local common = {whiteBlackCheckers, redBlackCheckers, woodPlanks, grass}
local uncommon = {polkaDots}

local tiers = {common, common, uncommon}

function onSelected()
	if player.leaderstats.PopPoints.Value >= 250 then
		local tier = tiers[math.random(1,#tiers)]
		local texture = tier[math.random(1,#tier)]
		if texture ~= floorTexture.Texture then
			floorTexture.Texture = texture



Server Script (Deducts points):

	player.leaderstats.PopPoints.Value = player.leaderstats.PopPoints.Value - 250

Any help or feedback is appreciated.

1 Like

Maybe try MouseButton1Down or MouseButton1Up and adding print statements to see if anything is being fired more than once? And also try using math.max so the value can never go under 0?

player.leaderstats.PopPoints.Value = math.max(player.leaderstats.PopPoints.Value - 250,0)

MouseButton1Up seems to be working, I’ll let you know if this changes.

1 Like