Script breaks when character dies and the humanoidrootpart falls into the vold

What is the issue?
I have a working script for a round system, This round system works great however with 1 slight overlook. It’s not that if you reset it won’t work it’s if the part on your character (HumanoidRootPart) falls into the Roblox void and gets deleted/destroyed, then it will cause the script to error out and worse. The script will stop the entire round system from working.

And so I need a way to avoid that from happening.

Don’t understand?
here’s a video that shows you.

local MapsFolder = game.ServerStorage.Maps
local Maps = MapsFolder:GetChildren()
local RoundTime = script.RoundTime
local IntermissionTime = script.IntermissionTime
local RoundTimeMinutes = 449--This number is here just to remember how much time there was.
local Lobby = game.ServerStorage.Lobby
local Players = game:GetService("Players")
local DS = game.Workspace.Derby_Sounds



game.Players.PlayerAdded:connect(function(Player)--When you join the game, you will either be teleported to the lobby or the maps
	Player.CharacterAdded:connect(function(Character)
		if script.GameRoundIsOn.Value == false then
			repeat wait() until Character.HumanoidRootPart
			Character.HumanoidRootPart.CFrame = Lobby.TeleportParts.Teleport_Point.CFrame--For the DevForms marked right here is the error
		end
	end)
end)



while true do
	script.GameRoundIsOn.Value = false
	Lobby.Parent = game.Workspace
	local ChoosenMap = Maps[math.random(1,#Maps)]
	local MapClone = ChoosenMap:Clone()
	
	--Picking a map to load-in.
	task.wait(IntermissionTime.Value)--How long until the next round starts
	game.Workspace.NextMapValue.Value = true
	game.Workspace.MapName.Value = (ChoosenMap.Name)
	print(ChoosenMap.Name.. " has been Choosen")
	
	--Map Choosen/Picked to load-in.
	task.wait(5)--Load-in (Delay)
	script.GameRoundIsOn.Value = true
	for i, Players in pairs(game.Players:GetChildren()) do
		local Char = Players.Character
		--Char.HumanoidRootPart.CFrame = MapClone[math.random(1,#MapClone)].CFrame + Vector3.new(0,5,0)
		local TeleportToSpawn = MapClone.TeleportParts:GetChildren()
		Char.HumanoidRootPart.CFrame = TeleportToSpawn[math.random(1, #TeleportToSpawn)].CFrame + Vector3.new(0,5,0)
		Char:WaitForChild("Humanoid").Jump = true
	end
	Lobby.Parent = game.ServerStorage
	game.Workspace.NextMapValue.Value = false
	MapClone.Parent = game.Workspace.InGame_Maps
	MapClone:MakeJoints()
	task.wait(RoundTime.Value)--The amount of time during that map
	DS.TimeLeft:Play()
	task.wait(2)--How much time you have left before map changes
	
	--Lobby&Teleport
	print("Times Up!")
	DS.TimeUp:Play()
	game.Workspace.TimesUpValue.Value = true
	task.wait(1.25)
	print("Next map")
	DS.Voice:Play()
	task.wait(3.5)--Loads in lobby&Teleports Everyone to the lobby.
	for i, Players in pairs(game.Players:GetChildren()) do
		local Char = Players.Character
		Char.HumanoidRootPart.CFrame = Lobby.TeleportParts.Teleport_Point.CFrame
		Char:WaitForChild("Humanoid").Jump = true
		DS.Lobby:Play()
	end
	task.wait(0.2)--Map being removed (Delay)
	MapClone:Destroy()
	game.Workspace.TimesUpValue.Value = false
end

You could do WaitForChild(), which will yield the script until the object is not nil.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.