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 1 year 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:

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)

if Coins.Value = 1000 then
    local RS = game:GetService("ReplicatedStorage")
    local Remote = RS.LockerPurchase

    Remote.OnServerEvent:Connect(function(Player)
        Player.Data.Dust.Value = Player.Data.Dust.Value - 1000
        wait(0.1)
    end
end)

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

3 answers

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LockerPurchase = ReplicatedStorage.LockerPurchase

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
    Coins:GetPropertyChangedSignal("Value"):Connect(function() -- when the value is changed
        local NewValue = Coins.Value
        local Aftermath = math.clamp(NewValue, 0, math.huge) -- incase if a script sets the value to a negative
        Coins.Value = Aftermath
    end)
end)

LockerPurchase.OnServerEvent:Connect(function(Player)
    local Data: Folder = Player:FindFirstChild("Data")
    local Coins: IntValue = Data:FindFirstChild("Dust")

    if Data and Coins then -- just to double check if they exist
        if Coins.Value >= 1000 then -- if the player's "Dust" is more than/equal to 1000
            Coins.Value -= 1000 -- subtract 1000 from its value
        end
    end
end)

Make sure this is a normal script inside ServerScriptService.

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

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

money = 50
Cost = 100

Aftermath = money - cost -- -50

if Aftermath < 0 then
    --Cannot buy
else
    --Can buy
end

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:

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)

    if Coins.Value = 1000 then
        local RS = game:GetService("ReplicatedStorage")
        local Remote = RS.LockerPurchase

        Remote.OnServerEvent:Connect(function(Player)
        Aftermath = coins - 1000
        if Aftermath >= 0 then
               Player.Data.Dust.Value = Player.Data.Dust.Value - 1000
        else
            -- not enough
            wait(0.1)
        end
    end)

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 — 1y
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 — 1y
0
im quite confused at the code, sorry if i cant help any further JP_Pontes 5 — 1y
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 — 1y
Log in to vote
1
Answered by 1 year ago

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

math.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.

Aftermath = 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 — 1y

Answer this question