Players having less than 0 gold?

local awardGold = Instance.new("BindableEvent", script.Parent)
awardGold.Name = "awardGold"

awardGold.Event:connect(function(player, goldGiven)
	if player and goldGiven then
		local myData = playerData[player.userId]
	    if myData then
			if gamePassService:PlayerHasPass(player, goldID) then 
				myData.gold = myData.gold + (goldGiven * 3)
				local goldValue = player.PlayerFolder:WaitForChild("Gold")
				if goldValue then
					goldValue.Value = myData.gold
				end
			else
				myData.gold = myData.gold + goldGiven
				local goldValue = player.PlayerFolder:WaitForChild("Gold")
				if goldValue then
					goldValue.Value = myData.gold
				end
			end
		end
	end 
end)

if myData.gold >= cost then
			script.Parent.awardGold:Fire(player, -cost) -- Taking away the gold from their balance
			purchase(item, itemName, player)
			buyEvent:FireClient(player, true)
end

I’m having problems with my gold awarding system. Players have informed me that they have gotten negative amounts of money, which should be impossible. I check to see if myData.gold >= cost, which myData is the data store. and then cost is the, well cost. So logically the awardGold event shouldn’t be fired unless they have enough money. Is there some way I can check to see why this is happening? I haven’t seen it myself, but I’ve had players SS and show me, so I know that some how players are getting less than 0 gold.

1 Like

Is your game experimental mode? If it is, somehow they (or their local scripts) could be modifying the gold so it is below 0. You should also look through all of your server-side scripts to make sure it’s not one of them, either.

But, just as a precaution, clamp the value of the gold every time you change it (or it gets changed), for example:

Gold:GetPropertyChangedSignal("Value"):Connect(function()
    Gold.Value = math.clamp(Gold.Value, 0, math.huge)
end)

No it’s not in experimental mode, but I’ll put in that piece of code and see how it goes!

2 Likes

So if they have the gamepass they also have to pay triple the price?
That is a major oversight and would also explain why people go into the negative.

2 Likes

And this seems, to me, exactly what your issue is too, @NinjoOnline.

First of all, big oversight on making players spend more for owning the gamepass. However, in the future, make sure it checks how much they will have to spend, and not the price of it. By checking the price rather than how much the player spends, it forces you to rearrange your system later if you end up adding things that could potentially lower the price of an item.


However, back onto your issue, an easy fix would be to switch this:

if gamePassService:PlayerHasPass(player, goldID) then

To this:

if goldGiven > 0 and gamePassService:PlayerHasPass(player, goldID) then

This way it won’t multiply any negatives.

4 Likes

I see the issue.
You’re checking if balance-cost > 0, not balance-(cost*3) > 0

OMG, this was a massive oversight! The reason why gamepass was *3 is because I use that event to add gold to the players balance, so the pass was to give you 3 times the amount normally given. Completely didn’t realise that it was also affecting the negatives as well!

Thank you!! :smiley:

No need to clamp in that case, just use math.max

1 Like

Don’t you mean min? lol

And alright ty

Nope. Min picks the smaller number. Max picks the bigger number. I mix them up all the time (and edited my post a minute after I posted it because I did it again).

1 Like

Oh, I see. I thought it was just a one-sided clamp.

I really need to study the math library again lol

1 Like

math.min and math.max both take infinite number arguments, and will return the smallest and largest number from the given parameters respectively.

math.max(6, 3) --6

Or in this case…

math.max(coins, 0)

1 Like

They actually only take up to 8000 arguments (an important lua limitation to know about if you ever find yourself unpacking arbitrary-length tables.)

for argCount = 7995, 8005 do
	print("Testing", argCount)
	local list = {}
	for i = 1, argCount do
		list[i] = i
	end
	print("Result", math.max(unpack(list)))
end
3 Likes