How to locate "Humanoid"

I’m currently working on an infinite fall game as a scripting practice. I’m really new to scripting in lua on Roblox.

I’m trying to locate the “Humanoid” in the player. Can someone please help me?

Here’s my script (ignore the comments)

3 Likes

This would not work, OP is using a server script, you cannot access LocalPlayer from a server script. There’s also a slight syntax error in your code.

@reallyyellie What is the goal you are trying to achieve with your script so we can further assist you?

1 Like

I want it so when the player touches the part, then they will be teleported back up to the start.

Right, so we can use an event called a Touched event for this. A Touched event fires when something collides with that part. In this case, when the player touches the part, we can find the player and teleport him back up to the start.

local spawnPart = workspace.spawnPart -- You can make a part named spawnPart in workspace and position it where you want the player to start

local part = workspace.Part -- The part that will be touched, once again it can be any part in workspace

part.Touched:Connect(function(hit)
	if hit then
		local char = hit:FindFirstAncestorWhichIsA("Model") -- Find the character
		local rootPart = char:FindFirstChild("HumanoidRootPart") -- Find the HumanoidRootPart
		
		if rootPart then -- Check if the HumanoidRootPart exists
			-- If it does exist then we can change the position of the rootpart and place it at the spawn
			rootPart.CFrame = spawnPart.CFrame
		end
	end
end)

I made this in quite a rush, apologies if there is an error.

7 Likes

do

local LoopPart = game.Workspace.Part -- Make sure this is an unique name! (there is no other parts with the name "Part")

local Hole = game.Workspace.Union -- Read above.

    game.Players.PlayerAdded:Connect(function(plr) -- This detects a player entering the game, and assigns the variable plr to him.
           plr.CharacterAdded:Connect(function(character) -- Detects character.
           local humanoid = character:FindFirstChild("Humanoid")
                 -- Code
           end)
    end)
6 Likes

Fixing your script: If a Model touches the part that is NOT the player, the script will error.

Fix:

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local char = hit.Parent
		local rootPart = char:FindFirstChild("HumanoidRootPart") -- Find the HumanoidRootPart
		
		if rootPart then -- Check if the HumanoidRootPart exists
			-- If it does exist then we can change the position of the rootpart and place it at the spawn
			rootPart.Position = spawnPart.Position
		end
	end
end)
2 Likes

That seems like an unnecessary amount of checks. Humanoid.Torso is deprecated and I would not recommend using deprecated code.

local LoopPart = game.Workspace.Part
local Hole = game.Workspace.Union

LoopPart.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- The player that touched the part
    local hum = hit.Parent:FindFirstChild("Humanoid") -- Finds the Humanoid in the Player's Character.
    if hum then -- Checks if Humanoid exists
        -- Your code here
    end
end)
1 Like

you will need to get the player for client scripts you can do:

local player = game.Players.LocalPlayer 

for server scripts(like this one) you have to do

local Players = game:GetService("Players")

--for a certain user
local player = Players:FindFirstChild("UsernameGoesHere") --player needs to be in-game when the code runs
print(player.Name)

--for all the users
for i, player in pairs(Players:GetPlayers()) do --loops through all the users in-game when the code runs
	print(player.Name) 
end

Players.PlayerAdded:Connect(function(player) --fires when a player joins the game
	print(player.Name)
end)

Players.PlayerRemoving:Connect(function(player) --fires when a player leaves the game
	print(player.Name)
end)

--gets the Character
local character = player.Character

--gets the Humanoid
local humanoid = character.Humanoid

--if character or humanoid is nil it means you need to wait for them to load or they somehow got deleted

hope I helped!

5 Likes

Okay, you’re using a Server Script. I know a way to get the character from a Server Script:


--//Players\\--


local Players = game:GetService('Players')


--//Scripting\\--


    Players.PlayerAdded:Connect(function(Player)


    	Player.CharacterAdded:Connect(function(Character)


			local Humanoid = Character:FindFirstChildWhichIsA('Humanoid')


    	end)


    end)

It’s just it, I hope I helped ya!


  • Tsu Washington / SovietFurryBruh.
2 Likes

Finding the character from a descendant like accessories can be challenging. I use the script below to find character. It is a Server-side script in ServerScriptService.

I also recomemnd you to use GetService for the services because service names could change.

local _Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local Part = _Workspace:WaitForChild("KillPart")

local BodyPartsForR6 = { "Head", "Left Arm", "Left Leg", "Right Arm", "Right Leg", "Torso" }
local BodyPartsForR15 = { "Head", "LeftFoot", "LeftHand", "LeftLowerArm", "LeftLowerLeg", "LeftUpperArm", "LeftUpperLeg", "LowerTorso", "RightFoot", "RightHand", "RightLowerArm", "RightLowerLeg", "RightUpperArm", "RightUpperLeg", "UpperTorso" }

local function GetCharacterFromPart(OtherPart:BasePart)
	if table.find(BodyPartsForR6, OtherPart.Name) or table.find(BodyPartsForR15, OtherPart.Name) then
		local Character = OtherPart.Parent
		local Player = Players:GetPlayerFromCharacter(Character)

		print(string.format("\n\nCharacter: %s\nPlayer: %s\nCollided Part: %s\n\n", tostring(Character:GetFullName()), tostring(Player:GetFullName()), tostring(OtherPart:GetFullName())))
		return Character

		--Character is the parent of the otherPart
		--because otherPart is a limb.
	end
end

Part.Touched:Connect(function(OtherPart)
	local Character = GetCharacterFromPart(OtherPart)
	if Character then
		local Humanoid = Character:FindFirstChild("Humanoid")
		if Humanoid and Humanoid.Health > 0 then
			Humanoid:TakeDamage(Humanoid.Health)
		end
	end
end)

You can also use this place as an example:
GetCharacterFromPart.rbxl (89.1 KB)

2 Likes