I’m trying to fix this error, when I play, I get:
Argument 2 missing or nil
In addition, I’m not sure how to test a PlayerRemoving event.
Here is my code:
local DataStoreService = game:GetService("DataStoreService")
local CurrentLevels = DataStoreService:GetDataStore("CurrentLevels")
local function AddBoard2Player(player)
local board = Instance.new("Model",player)
board.Name = "leaderstats"
local money = Instance.new("IntValue", board)
money.Name = "Level"
money.Value = 1
local data
local succes, errormessage = pcall(function()
data = CurrentLevels:SetAsync(player.UserId.."-Level")
end)
if succes then
money.Value = data
else
print("Error saving")
warn(errormessage)
end
end
local function SaveData(player)
local succes, errormessage = pcall(function()
CurrentLevels:SetAsync(player.UserId.."-Level",player.leaderstats.Level.Value)
end)
if succes then
print("PlayerData Saved!")
else
print("There was a problem saving the data")
warn(errormessage)
end
end
game.Players.PlayerAdded:Connect(AddBoard2Player)
game.Players.PlayerRemoving:Connect(SaveData)
data = CurrentLevels:SetAsync(player.UserId.."-Level")
:SetAsync()
must have a second parameter and you didn’t provide any.
I tried adding a parameter, now my level value is set to zero and doesn’t print or warn anything at all
local function AddBoard2Player(player)
local board = Instance.new("Model",player)
board.Name = "leaderstats"
local money = Instance.new("IntValue", board)
money.Name = "Level"
money.Value = 1
local data
local succes, errormessage = pcall(function()
data = CurrentLevels:SetAsync(player.UserId.."-Level",player.leaderstats.Level.Value)
end)
if succes then
money.Value = data
else
print("Error saving")
warn(errormessage)
end
end
I want to ask real quick, are you trying to get data from datastore or write data to datastore?
Write data to a datastore
Because :SetAsync()
doesn’t return anything so data
variable is always nil
.
Then how else would I save the data?
:SetAsync()
does save the data but it doesn’t return anything as argument when it finishes it’s job so there is no need for that data
variable.
Unless you intended to use :GetAsync()
which only takes one parameter and returns the available data from the given key.
1 Like
I kept the data
variable but changed SetAsync()
to GetAsync()
now it works, thanks