I am creating a datastore for a game i’m working on and have decided to create the datastore differently to how I usually would to include pcall to make it more secure. I have errors with “attempt to index nil with number” & “attempt to index nil with ‘UserId’” and im unsure on how to fix these having never really used pcall before.
Ive attached a screenshot of the errors and my datastore script in full. Any help appreicated!
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("RadarData")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "PlayerData"
local Currency = Instance.new("IntValue",leaderstats)
Currency.Name = "RadarTokens"
Currency.Value = 0
local Exp = Instance.new("IntValue",leaderstats)
Exp.Name = "ATCExp"
Exp.Value = 0
local data
local success, errormessage = pcall(function()
data = DS:GetAsync(player.UserId)
end)
if success then
player.PlayerData.RadarTokens.Value = data[1]
player.PlayerData.ATCExp.Value = data[2]
else
error("Error gathering user data. Please report in our comms server. " .. errormessage)
end
end)
Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function(player)
DS:SetAsync(player.UserId, {player.PlayerData.RadarTokens.Value, player.PlayerData.ATCExp.Value})
end)
if success then
print("Player data saved successfully.")
else
error("There was an error saving user data. " .. errormessage)
end
end)