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

Saving table with DataStore2?

Asked by
Filipalla 504 Moderation Voter
5 years ago
Edited 5 years ago

If i click the button that Fires PurchaseCar two times i don't get 'Player has car' in the output but it will print out 'Filipalla Purchased Car' for the first click.

This makes me think it does save it because otherwise it would print that as many times as i press the button and i don't understand why it won't print 'Player has car'.

Edit: Documentation of DataStore2 can be found here.

Edit: I made some changes to the code and now carDataStore:Get() is nil. Now i know it saves correctly because when i play again it's still nil.

OnCarPurchase

local DataStore2 = require(1936396537)

local CarsAPI = require(game.ServerStorage.CarsAPI)

local Money = 1000000000000--Where your money value is located

game.ReplicatedStorage.PurchaseCar.OnServerEvent:Connect(function(Player, CarModelName)

    local carDataStore = DataStore2("OwnedCars", Player)

    if CarsAPI.Cars[CarModelName] then

        if typeof(carDataStore:Get()) == "table" then

            local OwnedCars = carDataStore:Get()

            if OwnedCars[CarModelName] then

                warn("Player has car")

            else

                if Money > CarsAPI.Cars[CarModelName].Price then

                    local TableWithAddedCar = table.insert(OwnedCars, #OwnedCars + 1, CarModelName)

                    carDataStore:Set(TableWithAddedCar)

                    print(Player.Name.." Purchased "..CarModelName)

                else

                    warn(Player.Name.." Doesn't have enough money to buy "..CarModelName)

                end

            end

        end

    end

end)

DataStore2Script

local DataStore2 = require(1936396537)

local defaultvalue = {}

local OwnedCars

game.Players.PlayerAdded:Connect(function(plr)

    local carDataStore = DataStore2("OwnedCars", plr)

    local function carsUpdate(updatedValue)

        OwnedCars = carDataStore:Get(updatedValue)

    end

    carsUpdate(defaultvalue)

    carDataStore:OnUpdate(carsUpdate)

end)
0
try table.insert(defaultvalue, carDataStore:GetChildren()) iiDev_Hunt3r 64 — 5y
0
That won't work. Filipalla 504 — 5y

Answer this question