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

How can I keep my leaderstat gem value from going below zero, when buying potions?

Asked by
R3M_1 11
1 year ago

Using, DataStore2

So, I'm setting up a store Gui and when you hit the buy button a local script activates a remote event., which then fires in my leaderstat script, and it takes 20 away from the gems value. The problem is, is that I can't seem to figure out how to make it so that it the player doesn't have 20 gems then they can't buy it, as it is now, it just goes -20, -40 and so on, I tried an if statement a couple of different, i tried it once in the local script, so that if gems.Value >= 20 then fire the event, but it fires wether there is 20 gems or not, then i tried in the leaderstat script, like, if gems.Value >= 20 then gemStore:Increment(-20) but it didnt work either, heres the two scripts without either if statements!

leaderstat Script

local Players = game:GetService('Players')
local ServerScriptService = game:GetService('ServerScriptService')
local Workspace = game:GetService("Workspace")
local StarterGui = game:GetService("StarterGui")

local DataStore2 = require(ServerScriptService.DataStore2)


DataStore2.Combine("DATA", "clicks", "rebirths", "gems")
Players.PlayerAdded:Connect(function(player)
    local clickStore = DataStore2("clicks", player)
    local rebirthStore = DataStore2("rebirths", player)
    local gemStore = DataStore2("gems", player)


    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

    local clicks = Instance.new("IntValue", leaderstats)
    clicks.Name = "Clicks"
    clicks.Value = clickStore:Get(0)

    local rebirths = Instance.new("IntValue", leaderstats)
    rebirths.Name = "Rebirths"
    rebirths.Value = rebirthStore:Get(0)

    local gems = Instance.new("IntValue", leaderstats)
    gems.Name = "Gems"
    gems.Value = gemStore:Get(0)


    clickStore:OnUpdate(function(newClicks)
        clicks.Value = newClicks
    end)

    rebirthStore:OnUpdate(function(newRebirths)
        rebirths.Value = newRebirths
    end)

    gemStore:OnUpdate(function(newGems)
        gems.Value = newGems
    end)


end)


game.ReplicatedStorage.AddClick.OnServerEvent:Connect(function(player)
    local multiplier = player:WaitForChild("multiply"):FindFirstChild("ClickMultiplier").Value
    local potionX = player:WaitForChild("multiply"):WaitForChild("PotionX").Value
    local clickStore = DataStore2("clicks", player)
    clickStore:Increment(multiplier + potionX)
end)

game.ReplicatedStorage.AddRebirths.OnServerEvent:Connect(function(player)

    local gemStore = DataStore2("gems", player)
    local clickStore = DataStore2("clicks", player)
    local clicks = player:WaitForChild("leaderstats"):WaitForChild("Clicks")
    local rebirthStore = DataStore2("rebirths", player)
    rebirthStore:Increment(1)
    clickStore:Set(0)
    gemStore:Increment(10)


end)
-- takes gems away from overall value!!!
game.ReplicatedStorage.gemMultiply.OnServerEvent:Connect(function(player)

    local gemStore = DataStore2("gems", player)

    gemStore:Increment(-20)


end)


game.Workspace.ResetStat.ClickDetector.MouseClick:Connect(function(player)
    local gemStore = DataStore2("gems", player)
    gemStore:Set(0)
end)

localscript in Potion Store Gui

script.Parent.MouseButton1Click:Connect(function()
        game.ReplicatedStorage.gemMultiply:FireServer()


end)

1 answer

Log in to vote
0
Answered by
Puppynniko 1059 Moderation Voter
1 year ago
Edited 1 year ago

i think this should work for you Gem is the Gem count

local buyPrice = 20
game.ReplicatedStorage.gemMultiply.OnServerEvent:Connect(function(player)
    local gemStore = DataStore2("gems", player)
local Gems = player.leaderstats.Gems.Value
   if Gems > buyPrice or Gems == buyPrice then
        gemStore:Increment(-buyPrice)
else
print("Player dont have enough gems")
end

end)
0
would i place that in my leaderstat script or, in my local script that fires the Event? R3M_1 11 — 1y
0
I put it in my leaderstat script and I call for Gem = player:WaitForChild("leaderstats"):WaitForChild("Gems") then i make a local buyPrice = 20 which is the cost of the potion, then i set up the code you gave me, and it does nothing, but if i put else print("not enough") itll print that even if my gem count shows 20. I also changed it to gem.Value > buyPrice in the code you gave. R3M_1 11 — 1y
0
ive made a button that resets the DataStore2 gem data to 0 incase it just bugs but even after using and getting 20 gems again, it still just says not enough. R3M_1 11 — 1y
0
wait let me give you the full in server script Puppynniko 1059 — 1y
View all comments (5 more)
0
there i edited it this script will check if the players Gem Count is Above 20 and one thing to note is that if the player have exactly 20 gems it would not work so you would want to add  a or player.leaderstats.Gems.Value == 20 to see if the player have the exact amount needed to buy the potion Puppynniko 1059 — 1y
0
are you testing changing the value in the client? Puppynniko 1059 — 1y
0
It absolutely worked, I just didn't see the "or" when I read the if Statement, I Appreciate the Help!! R3M_1 11 — 1y
0
another alternative is to use math.Clamp if some how the player bypassed the if statements Puppynniko 1059 — 1y
0
But one more thing, why does that work when it's just > or == rather than just >=. I tried that exact thing with >= but not once did i think of using an "or" R3M_1 11 — 1y
Ad

Answer this question