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 4 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:

01local Datastore2 = require(1936396537)
02local cost = 1500
03local petModule = require(game.ServerScriptService:WaitForChild("PetModule"))
04script.Parent.ClickDetector.MouseClick:Connect(function(player)
05 
06    if player.leaderstats.Coins.Value >= cost then
07 
08        local CoinsStore = Datastore2("Coins", player)
09        local Coins = player.leaderstats.Coins
10 
11        CoinsStore:Set(Coins.Value - 1500)
12 
13        local pet = petModule.chooseRandomPet()
14 
15        print(pet.Name.." was selected.")
16    end
17 
18 
19end)
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 — 4y
0
Why don't you edit the leaderstats instead of the datastore? User#30567 0 — 4y
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 — 4y

1 answer

Log in to vote
2
Answered by 4 years ago

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

1-- how you do it
2Coins.Value = Coins.Value - 1500
3 
4-- not how you do it
5Coins.Value = -1500

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

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

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

01local Datastore2 = require(1936396537)
02local cost = 1500
03local petModule = require(game.ServerScriptService:WaitForChild("PetModule"))
04script.Parent.ClickDetector.MouseClick:Connect(function(player)
05 
06    if player.leaderstats.Coins.Value >= cost then
07 
08        local CoinsStore = Datastore2("Coins", player)
09        local Coins = player.leaderstats.Coins
10 
11         CoinsStore:Set(Coins.Value = Coins.Value - 1500)
12 
13        local pet = petModule.chooseRandomPet()
14 
15        print(pet.Name.." was selected.")
16    end
17 
18 
19end)

Hope this helped!

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

Answer this question