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

Why does my script say " Argument 2 missing or nil" in my Data Store script?

Asked by 4 years ago

I have created a Data Store script but when i click Play it does not work and always says "Argument 2 missing or nil"

local DS = game:GetService("DataStoreService")
local myDS = DS:GetDataStore("test")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name  = "leaderstats"
    leaderstats.Parent = player

    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Parent = leaderstats

    local data
    local success, errormessage = pcall(function()
        data = myDS:setAsync(player.UserId.."-cash")
    end)
    if success then
        cash.Value = data
    else 
        print("There was an error whilst getting your data")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
        myDS:SetAsync(player.UserId.."-cash", player.leaderstats.cash.Value)
    end)
    if success then
        print("Player Data success saved!")
    else
        print("There was an error when saving data")
        warn(errormessage)
    end
end)
2
The method :SetAsync() is to be used only for writing* data, it requires two arguments: The Data-Key, and the Data to be written: on line 15 you're missing this argument. Ziffixture 6913 — 4y
1
Regardless, you're calling the incorrect function. You're looking for :GetAsync(). Ziffixture 6913 — 4y
1
You shouldn't be using :SetAsync() too much either, as it overrides any previous data, if your Cash value is an integer, :IncrementAsync() is something you should take a look at. Ziffixture 6913 — 4y
0
@Feahen Thank you so much! TotallyNotChupper100 21 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Like the person said in the comments, SetAsync requires 2 arguments and you gave it only one

i think you ment GetAsync instead and just made a typo on line 15

Ad

Answer this question