Help on checking instance for a part

I’m trying to make a script check when a instance of the character isnt there, and if it isnt there it’ll reload the character. this is the first time im handling something of this nature so this is the script i came up with

while  true do
	wait()
	local character = game.Players.Username.Character

	local f = character:FindFirstChild("UpperTorso")
	if f ~= nil then
		game.Players.Username:LoadCharacter()
	end
end

username is just where i put in my username

This isn’t a very good idea, you would just have the Character constantly loading as its checking if the Instance is not nil, not to mention that the Character takes time to load, so you would have the Character be Loading, for no reason, it is recommend you use WaitForChild() instead, But Instead of
if f ~= nil then, just do, if f then, to check if nil, do if not f then

local character = game.Players[Username].Character

You can also use Player.CharacterAppearenceLoaded for this instead

I’d use Instance.ChildRemoved, this one fires when a child is removed. You could check if the child removed is a basePart, and if so, load the character.

This code, every time a part disappears on the character it runs a function which checks if that part should be in the character. If so, It’ll load the character. You have to specify which ClassNames you want to check. I added some by default, but you might want to add “Tool”, or any other thing.

It’d end up looking something like this:

char = game.Players.Username.Character
check_for = { -- Add here all the elements classNames you want to check.
  "BasePart",
  "Accessory",
  "Shirt",
  "Pants"
}

char.ChildRemoved:Connect(function(child)
  should_be = table.find(check_for, child.ClassName)

  if should_be then
    game.Players.Username:LoadCharacter()
  end
end)

Ideally you don’t want to use a while loop to do this and instead use Roblox signals. A way you could do this is by connecting either the ChildRemoving or DescendantRemoving connection to the character and then checking if the part being removed should trigger the character to respawn. The post above also explains this.

Example code:

local Players = game:GetService("Players")

local partNamesToCheck = {
	"UpperTorso",
}

local function onPlayerAdded(player: Player)
	local function onCharacterAdded(character: Model)	
		local connection
		-- Change ChildRemoved to DescendantRemoving if you want to detect all descendants of the character
		connection = character.ChildRemoved:Connect(function(object)
			if table.find(partNamesToCheck, object.Name) then
				player:LoadCharacter()
			end
		end)

		-- Remove connections when characters parent changes to nil
		character.AncestryChanged:Connect(function()
			if character.Parent == nil then
				connection:Disconnect()
			end
		end)
	end

	player.CharacterAdded:Connect(onCharacterAdded)

	-- If character is already loaded then fire onCharacterAdded
	if player.Character then
		task.spawn(onCharacterAdded, player.Character)
	end
end

-- Handles player adding
Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(onPlayerAdded, player)
end

This ended up working, but i revised it a little


local char = game.Players.Player.Character

local check_for = { 
"UpperTorso",

"HumanoidRootPart",

"LeftUpperLeg",

"RightUpperLeg"

}

while wait() do

char.ChildRemoved:Connect(function(ChildRemoved)

local should_be = table.find(check_for,"UpperTorso" or "HumanoidRootPart" or "LeftUpperLeg" or "RightUpperLeg")

if should_be then

game.Players.Player:LoadCharacter()

end

end)

end```
1 Like

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