Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make currency decrease without making it go into negatives?

Asked by 2 years ago

So I am developing a Roblox game that relies on purchasing cosmetics and other items. To get the currency for these items, there's classes in the game that you can attend and play mini games to get money. The starter amount of money you have when you first join the game is $50. So scripting the actual getting the money is good, it's purchasing items that I need help with.

I'm using the same script we used for getting money, except instead of having "+(number of currency) at the bottom, we replaced the + with a - to decrease the value. But because the starting value is 50, you can still purchase it for 1,000, it will just decrease your money to -950. But I don't want that happen. I want to make it so if you try to purchase it but you don't have enough money for it, it won't purchase and your money won't decrease. I tried scripting this myself but I don't have the best knowledge of how scripts in Roblox work. I added "if Coins.Value = 1000 then" above the actual part of the script that decreases money, as an attempt to make it so it will only decrease if the amount of money you have in 1,000.

Additional Info: This script is for a GUI Button. The purchasing happens when you click on the button. This script is a script in ServerScriptService and "RS" is the variable for Replicated Storage.

Here's the script:

01game.Players.PlayerAdded:Connect(function(Player)
02    local Data = Instance.new("Folder", Player)
03    Data.Name = "Data"
04 
05    local Coins = Instance.new("IntValue", Data)
06    Coins.Name = "Dust"
07    Coins.Value = 50
08end)
09 
10if Coins.Value = 1000 then
11    local RS = game:GetService("ReplicatedStorage")
12    local Remote = RS.LockerPurchase
13 
14    Remote.OnServerEvent:Connect(function(Player)
15        Player.Data.Dust.Value = Player.Data.Dust.Value - 1000
16        wait(0.1)
17    end
18end)

If you have any ideas that could help, please let me know! <3

3 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Combining @JP_Pontes and @CocoDysphoria's answers, the final script should look like this:

01local Players = game:GetService("Players")
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03 
04local LockerPurchase = ReplicatedStorage.LockerPurchase
05 
06Players.PlayerAdded:Connect(function(Player)
07    local Data = Instance.new("Folder", Player)
08    Data.Name = "Data"
09 
10    local Coins = Instance.new("IntValue", Data)
11    Coins.Name = "Dust"
12    Coins.Value = 50
13    Coins:GetPropertyChangedSignal("Value"):Connect(function() -- when the value is changed
14        local NewValue = Coins.Value
15        local Aftermath = math.clamp(NewValue, 0, math.huge) -- incase if a script sets the value to a negative
View all 29 lines...

Make sure this is a normal script inside ServerScriptService.

0
This worked! Thank you all so much!! BloomFlame1 2 — 2y
Ad
Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

check what the value is and make a value that will state what it will be after the change:

01money = 50
02Cost = 100
03 
04Aftermath = money - cost -- -50
05 
06if Aftermath < 0 then
07    --Cannot buy
08else
09    --Can buy
10end

my guess is that this would work, but make sure to test it to make sure, i hope i helped you ah i see, try something like this:

01game.Players.PlayerAdded:Connect(function(Player)
02        local Data = Instance.new("Folder", Player)
03        Data.Name = "Data"
04 
05        local Coins = Instance.new("IntValue", Data)
06        Coins.Name = "Dust"
07        Coins.Value = 50
08    end)
09 
10    if Coins.Value = 1000 then
11        local RS = game:GetService("ReplicatedStorage")
12        local Remote = RS.LockerPurchase
13 
14        Remote.OnServerEvent:Connect(function(Player)
15        Aftermath = coins - 1000
View all 22 lines...

tell me if it works

0
Thanks for the help! Though, I'm still pretty confused. Where exactly in the script am I supposed to add this, or should I scrap my current script completely and replace it with what you suggested? I'm not sure. Here's the new version of the script if you can help see what's wrong with it. I tried testing it but it didn't work: BloomFlame1 2 — 2y
0
game.Players.PlayerAdded:Connect(function(player) local Data = Instance.new("Folder", Player) Data.Name = "Data" local Coins = Instance.new("IntValue", Data) Coins.Name = "Dust" Coins.Value = 50 end) money = Coins.Value cost = 1000 Aftermath = money - cost if Aftermath < 0 then --Cannot Buy else --Can Buy end BloomFlame1 2 — 2y
0
im quite confused at the code, sorry if i cant help any further JP_Pontes 5 — 2y
0
try checking what is going to be left if the player clicks the button, and then check if the "aftermath" is negative, if it is, tell the player he does not have enough, if he does have enough, then continue with the purchase JP_Pontes 5 — 2y
Log in to vote
1
Answered by 2 years ago

Pretty easy! math.clamp() is the function you're looking for! It works like this:

1math.clamp(number,minimum,maximum)

This'll return a number inbetween the minimum and maximum. If the number is already inbetween, it'll stay the same and nothing will change (like math.clamp(5,0,10) would be 5) however if it's below the minimum it'll be set to the minimum. If you have no upper limit on cash, you could set the minimum to 0 and the maximum to math.huge() for an infinite upper limit.

1Aftermath = math.clamp(money,0,math.huge())

Hope this helps!

0
Woah! I never knew that function exists up until now! This is useful for me! Thanks! T3_MasterGamer 2189 — 2y

Answer this question