Hello, i wants to make obby with leaderstats("stage"). I make it and open in settings API request, but it write error

ServerScriptService.Script:12: attempt to index nil with 'Stage'

I dont know how to fix it. What to do? My code:

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats
	stage.Value = DataStore:GetAsync(plr.UserId).Stage or 1
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DataStore:SetAsync(plr.UserId, {
		["Stage"] = plr.leaderstats.Stage.Value
	})
end)

1 comment

1

DataStore:GetAsync(plr.UserId) is nil, it does not exist. https://developer.roblox.com/en-us/articles/Data-store read this, might help, you are not using pcalls and not checking if data exists.