Why Is My Cash Pickup Giving More Than The Original Value?

I Have A System In My Game That Drops $50 Of The Players Money Whenever The Player Dies But The Issue Comes When The Player Picks Up The Cash Its Only Supposed To Give The Player $50 But Instead It Gives Them More EXAMPLE: Lets Say I Have $1000 And I Died So I Drop $50 So I Have $950 Then When I Go To Pick Up The Cash My Cash Goes From $950 To $1050 I Have No Idea Why I Am Using A Proximity Prompt To Pick Up The Cash Here Is The Script:

local part = script.Parent

local prompt = part.ProximityPrompt

local function give(player)

player.leaderstats.Money.Value += 50

part:Destroy()

end

prompt.Triggered:Connect(give)

This Is A Very Basic Script And I Can’t Seem To Find Where The Issue Is, Thanks!

You should attempt to disable the prompt, or have a true/false check.

local picked_up = false

local part = script.Parent

local prompt = part.ProximityPrompt

local function give(player)
if not picked_up and prompt.Enabled then
picked_up = true
prompt.Enabled = false
player.leaderstats.Money.Value += 50
part:Destroy()
end


end

prompt.Triggered:Connect(give)

I dont use proximity prompts much, but i hope this works

Do =+ 50 / 2 or == 50 instead.

local part = script.Parent

local prompt = part.ProximityPrompt

local function give(player)

player.leaderstats.Money.Value += 50 / 2 or player.leaderstats.Money.Value == 50 -- Should fix the bug.

part:Destroy()

end

prompt.Triggered:Connect(give)
2 Likes

Have a if statement checking if the cash has been picked up.

Example:

local part = script.Parent
local proximityPrompt = part.ProximityPrompt

local HasBeenPickedUp = false

local function giveCashToPlayer(Player)
	if not HasBeenPickedUp then
		Player.leaderstats.Money.Value += 50
		part:Destroy()
	end
end

proximityPrompt.Triggered:Connect(giveCashToPlayer)

That kinda seems odd because why would you have to reward the player twice?

I Tried This But It Still Goes To $1050 For Some Reason

I Tried This But It Still Goes To $1050 For Some reason.

Are you keeping the cash at $1000?

Yeah I Want The Cash To Go From $950 To $1000 Instead of $1050

You can look at my post i did.

Link: Why Is My Cash Pickup Giving More Than The Original Value? - #3 by kdopdaux1803

1 Like

This:
+= 50 / 2 or player.leaderstats.Money.Value == 50
Resolves to:
+= 25 or true/false
In other words the line should just be:
+= 25

The real issue here is that for some reason the prompt’s Triggered event/signal is being connected twice (likely due to an oversight).

Actually, no the reason why the money was actually giving the double amount of it is the += thing.

So i did += (number) / (number) or ... .Value == (number).

Look at the title, it’s: “Why Is My Cash Pickup Giving More Then The Original Value”.

1 Like

+= 50 shouldn’t be giving 100 cash, so the function is clearly being executed twice erroneously.

+= 50 / 2 or player.leaderstats.Money.Value == 50
This doesn’t work how you think it works, 50 / 2 will never evaluate to false so the second part of that expression is redundant.
+= nil or player.leaderstats.Money.Value == 50 --would error, 'attempt to perform arithmetic add on number and boolean'.

It can give up to 100 cash because the script has += on it, so it’s adding the number that is per example: if the number that you have 60, it’s giving 120 instead of 60. So this is why you have to do += 60 / 2.

50 / 2 = 25.

So 50 x 2 / 2 = 50.

True, if it’s nil and then a number, it’s gonna force do error in the output.

So you have to do += nil or player.leaderstats.Money.Value == nil.

1 Like

You seem to be confused about how the += compound operator works, it simply adds a value to an existing value and returns the result of that addition, i.e;

local Cash = 950
Cash += 50
print(Cash) --1000

You also seem to be confused about how Boolean expressions work.

print(50 / 2 or "Hello world!") --'Hello world!' will never print because the first part of the expression '50 / 2' will always evaluate to true.

So you have to do += nil or player.leaderstats.Money.Value == nil.

This is incorrect, this would still error.

local Cash = 950
Cash += nil or true --Attempt to perform arithmetic add on number and boolean
local Cash = 950
Cash += nil or false --Attempt to perform arithmetic add on number and boolean

I fixed it

local part = script.Parent
local proximityPrompt = part.ProximityPrompt

local HasBeenPickedUp = false

local function giveCashToPlayer(Player)
	if not HasBeenPickedUp then
		Player.leaderstats.Money.Value += 50
		part:Destroy()
        HasBeenPickedUp = true
	end
end

proximityPrompt.Triggered:Connect(giveCashToPlayer)
1 Like

You can also do:

local part = script.Parent
local CashToGive = 50
local prompt = part.ProximityPrompt

local function give(player)

 player.leaderstats.Money.Value = CashToGive -- CashToGive is the amount of 50.

 part:Destroy()

end

prompt.Triggered:Connect(give)
1 Like