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:
01 | local Datastore 2 = require( 1936396537 ) |
02 | local cost = 1500 |
03 | local petModule = require(game.ServerScriptService:WaitForChild( "PetModule" )) |
04 | script.Parent.ClickDetector.MouseClick:Connect( function (player) |
05 |
06 | if player.leaderstats.Coins.Value > = cost then |
07 |
08 | local CoinsStore = Datastore 2 ( "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 |
19 | end ) |
To subtract, you have to tell you're subtracting from the value like that:
1 | -- how you do it |
2 | Coins.Value = Coins.Value - 1500 |
3 |
4 | -- not how you do it |
5 | Coins.Value = - 1500 |
So applying that to your script, i'd be:
1 | 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:
01 | local Datastore 2 = require( 1936396537 ) |
02 | local cost = 1500 |
03 | local petModule = require(game.ServerScriptService:WaitForChild( "PetModule" )) |
04 | script.Parent.ClickDetector.MouseClick:Connect( function (player) |
05 |
06 | if player.leaderstats.Coins.Value > = cost then |
07 |
08 | local CoinsStore = Datastore 2 ( "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 |
19 | end ) |
Hope this helped!