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

Coins will not subtract from the leaderstats using Datastore2?

Asked by 3 years ago

Heres my issue. I learnt to create an Egg Opening System for my game with some of my friends. But when you click the part it does not remove the coins from the players balance using the :Set function from Datastore2. I don't understand why since there is no error code. But at the same time if i use CoinsStore:Increment(-1500) then it would say "Attempt to perform arithmetic (add) on a nil and a number." Any help would be appreciated.

Heres my code:

local Datastore2 = require(1936396537)
local cost = 1500
local petModule = require(game.ServerScriptService:WaitForChild("PetModule"))
script.Parent.ClickDetector.MouseClick:Connect(function(player)

    if player.leaderstats.Coins.Value >= cost then

        local CoinsStore = Datastore2("Coins", player)
        local Coins = player.leaderstats.Coins

        CoinsStore:Set(Coins.Value - 1500)

        local pet = petModule.chooseRandomPet()

        print(pet.Name.." was selected.")
    end


end)
0
All scripts should be your own. Do not take a free model and expect us to fix it (in this case, datastore2) User#30567 0 — 3y
0
Why don't you edit the leaderstats instead of the datastore? User#30567 0 — 3y
0
Datastore2 is not considered a free model. If you think so, it'd make all the games in the front page "free modeled". It is made for people to use it, as it is way better than the normal datastore. User#32819 0 — 3y

1 answer

Log in to vote
2
Answered by 3 years ago

To subtract, you have to tell you're subtracting from the value like that:

-- how you do it
Coins.Value = Coins.Value - 1500

-- not how you do it
Coins.Value = -1500

So applying that to your script, i'd be:

 CoinsStore:Set(Coins.Value = Coins.Value - 1500)

I never used datastore2, but if they didn't change anything then that should work. Your entire script:

local Datastore2 = require(1936396537)
local cost = 1500
local petModule = require(game.ServerScriptService:WaitForChild("PetModule"))
script.Parent.ClickDetector.MouseClick:Connect(function(player)

    if player.leaderstats.Coins.Value >= cost then

        local CoinsStore = Datastore2("Coins", player)
        local Coins = player.leaderstats.Coins

         CoinsStore:Set(Coins.Value = Coins.Value - 1500)

        local pet = petModule.chooseRandomPet()

        print(pet.Name.." was selected.")
    end


end)

Hope this helped!

0
I was looking through his script and I can't believe I didn't notice that lol zane21225 243 — 3y
Ad

Answer this question