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