Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My obby stage saving system doesn't teleport the player?

I've been working on this save system for a few hours however for some reason it throws me the error of "19:38:14.934 - ServerScriptService.playerHandler:43: attempt to index local 'part' (a nil value)" Hoping somebody will know why Here is the script:

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

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

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

players.PlayerRemoving:connect(function(player)
	local err = savePlayerData(player)
	if err then print(err) end
end)

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

1 answer

*Assuming your saving script works

Instead of using (line 38)

local hum,hrp = char:WaitForChild("Humanoid"),char:waitForChild("HumanoidRootPart")

try using

repeat wait() until char.Humanoid
repeat wait() until char.HumanoidRootPart
local hum,hrp = char:FindFirstChild("Humanoid"),char:FindFirstChild("HumanoidRootPart")

3 comments

0

i'll try thank you

0

i mean that stopped the original error but now it throws the error " 16:59:21.817 - ServerScriptService.playerHandler:45: attempt to index local 'part' (a nil value)"

0

In line 42, you find the stage in workspace, but you're using the IntValue NAME. Change that to local part = workspace.obbyStages:FindFirstChild(stage.Value)

This discussion is preserved as it was. New answers, comments, and votes are closed.