I was trying to make an obby system checkpoint that saves your stage but it doesnt seem to work. This is what the output says:

RegularRival has no data 15:19:05.771 - ServerScriptService.PlrHandler:64: attempt to index local 'part' (a nil value)

15:19:18.962 - ServerScriptService.ObbyHandler:27: attempt to index local 'stage' (a number value)

These are the 2 script that i used:

ObbyHandler

local obbyStages = workspace:WaitForChild("ObbyStages")

for _,stage in pairs(obbyStages:GetChildren()) do
	
	stage.Touched:connect(function(hit)
		
		local hum
		
		if hit.Parent:FindFirstChild("Humanoid") then
			
			hum = hit.Parent.Humanoid
			
		end
		
		if hit.Parent and hit.Parent.Parent:FindFirstChild("Humanoid") then
			
			hum = hit.Parent.Parent.Humanoid
			
		end
		
		if hum then
			
			local plr = game.Players:GetPlayerFromCharacter(hum.Parent)
			
			local stage = plr.leaderstats.Stage.Value
			
			if tonumber(stage.Name) == plr + 1 then
				
				plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value + 1
				
			elseif tonumber(stage.Name) > plr + 1 then
				
				hum.Health = 0 
				
			end
			
		end
		
	end)
	
end

PlrHandler

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local saveDataStore = dataStoreService:GetDataStore("SaveDataTest")

local function savePlrData(plr)
	
	local success,err =	pcall(function()
		
		local saveData = {}
		
		for _,stat in pairs(plr.Leaderstats:GetChildren()) do
			
			saveData[stat.Name] = stat.Value
			
		end
		
		saveDataStore:SetAsync(plr.UserId,saveData)
	
	end)
	
	if not success then return err end
	
end

players.PlayerAdded:connect(function(plr)
	
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = stats
	
	local data = saveDataStore:GetAsync(plr.UserId)
		
	if data then
		
		print(data.Stage)
		
		for _,stat in pairs(stats:GetChildren()) do
			
			stat.Value = data[stat.Name]
			
		end
		
	else
		
		print(plr.Name .. " has no data")
		
	end
	
	plr.CharacterAdded:connect(function(char)
		
		local humanoid,hrp = char:WaitForChild("Humanoid"),char:WaitForChild("HumanoidRootPart")
		
		wait()
		
		if humanoid and hrp then
			
			if stage.Value == 0 then
				
				local part = workspace.ObbyStages:FindFirstChild(stage.Value)
				hrp.CFrame = part.CFrame + Vector3.new(0, 1, 0)
				
			end
			
		end
		
	end)
	
end)

players.PlayerRemoving:connect(function(plr)
	
	local err = savePlrData(plr)
	
	if err then print(err) end
	
end)

game:BindToClose(function()
	
	for _,plr in pairs(players:GetPlayers()) do
		
		local err = savePlrData(plr)
	
		if err then print(err) end
	
	end
	
	wait(2)
	
end)

By the way i am also new to scripting so sorry for the mistakes.

2 comments

0

In PlrHandler: local part = workspace.ObbyStages:FindFirstChild(stage.Value) returns nil, make sure it actually contains the given child or make sure to check your value if it's correct.

0

In ObbyHandler: local stage = plr.leaderstats.Stage.Value is the actual value of your leaderstats, afterwards you attempt to call .Name on a number, which is not allowed. Make sure stage is the actual leaderstats value itself, not the value of the leaderstats value.