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
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)
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)
+= 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.
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
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)
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)