Https 403 error data store invalid key

Script:

local ds1 = datastore:GetDataStore("Speed1SaveNew1Service","",dssoptions)
local ds2 = datastore:GetDataStore("Speed2SaveNewService")
local ds3 = datastore:GetDataStore("Speed3SaveNew2Service")

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder",plr)
    leaderstats.Name = "PlayerRecords"
    
    local speed1 = Instance.new("NumberValue",leaderstats)
    speed1.Name = "Time1"
    speed1.Value = ds1:GetAsync(plr.UserId) or 0
    
    local speed2 = Instance.new("NumberValue",leaderstats)
    speed2.Name = "Time2"
    speed2.Value = ds2:GetAsync(plr.UserId) or 0
    
    local speed3 = Instance.new("NumberValue",leaderstats)
    speed3.Name = "Time3"
    speed3.Value = ds3:GetAsync(plr.UserId) or 0
end)

1 Like

If you have never saved a value to the store with the players UserId this is what you will get. GetAsync doesn’t return nil if there is no value so the “or” won’t work. You should wrap your calls to GetAsync in a pcall, check for success then assign the appropriate value. Something like this:

local success, errOrVal = pcall(ds1:GetAsync(plr.UserId))
if success then
speed1.Value = errOrVal
else
speed1.Value = 0
end

You could probably wrap that in a helper function.

Alternatively, if all of the stored values are expected to be integer values then you can simply do the following:

local ds1 = datastore:GetDataStore("Speed1SaveNew1Service","",dssoptions)
local ds2 = datastore:GetDataStore("Speed2SaveNewService")
local ds3 = datastore:GetDataStore("Speed3SaveNew2Service")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "PlayerRecords"

	local speed1 = Instance.new("NumberValue",leaderstats)
	speed1.Name = "Time1"
	speed1.Value = tonumber(ds1:GetAsync(plr.UserId)) or 0

	local speed2 = Instance.new("NumberValue",leaderstats)
	speed2.Name = "Time2"
	speed2.Value = tonumber(ds2:GetAsync(plr.UserId)) or 0

	local speed3 = Instance.new("NumberValue",leaderstats)
	speed3.Name = "Time3"
	speed3.Value = tonumber(ds3:GetAsync(plr.UserId)) or 0
end)

Since tonumber() will return nil if the argument cannot be converted to a number.

1 Like