How to make an if part is child of player script?

I’m trying to make a script that checks if my part is a part of a player this is what i have so far.
if ---part is a child of a player--- then end
how would I do this?

Of the player character you mean? Because then wouldn’t something like this work?

game.Players.PlayerAdded:Connect(function(plr)
        plr.CharacterAdded:Connect(function(char)

       --whatever code u want here i guess

      -- when you wanna check if part is child of player character run:
       if part.Parent == char then
         -- ur code
       end
       end)
end)

wrote this on the devforum, but should work?

well you can check the first child of the player being the name of “part” or like @carisards said, you can check the parent of the part if its the player Instance

code example:

local player = -- your player
local part = -- your part
if player:FindFirstChild(part.Name) then
      -- the rest is here
end

for some reason this code did not work. i could give you all my code for some context? this is in a local script

local repstorage = game:GetService("ReplicatedStorage")

local remoteevent = repstorage:WaitForChild("Hit")

local function Ontrigger()
	local bullet = script.Parent.Bullet:Clone()
	local rotationz = bullet.Rotation.Z
	local roationy = bullet.Rotation.Y
	local rotationx = bullet.Rotation.X
	
	bullet.Transparency = 0
	bullet.Parent = game.Workspace
	bullet.WeldConstraint:Destroy()
	bullet.LinearVelocity.Enabled = true
	bullet.CanTouch = true
	
	local touching = false
	
	bullet.Touched:Connect(function(part)
		touching = true
		remoteevent:FireServer(part)
		print("touched ")
		print(part.Name)
		print(part.Parent.Name)
		bullet:Destroy()
		
	end)
	local timetillstop = 5
	local timeatm = 0
	local waittime = 0.05
	repeat
		print("not false")
		bullet.Rotation = Vector3.new(rotationx,roationy,rotationz)
		wait(waittime)
		timeatm = timeatm+waittime
	until touching == true or timeatm >= timetillstop
end


script.Parent.Activated:Connect(Ontrigger)

this is in server script service in a normal script

local repstorage = game:GetService("ReplicatedStorage")

local event = repstorage:WaitForChild("Hit")

event.OnServerEvent:Connect(function(player, part)
	local balloon = game.ReplicatedStorage.Balloon:Clone()
	balloon.Parent = game.Workspace
	local attach = Instance.new("Attachment")
	attach.Parent = part
	balloon.Position = part.Position
	balloon.RopeConstraint.Attachment0 = attach
	balloon.LinearVelocity.Enabled = true
	if player:FindFirstChild(part.Name) then
		print("iamhuman")
	end
	wait("10")
	balloon:Destroy()
	attach:Destroy()
end)

I updated the code to try a different approach but its still not working

local repstorage = game:GetService("ReplicatedStorage")

local event = repstorage:WaitForChild("Hit")

event.OnServerEvent:Connect(function(player, part)
	local balloon = game.ReplicatedStorage.Balloon:Clone()
	balloon.Parent = game.Workspace
	local attach = Instance.new("Attachment")
	attach.Parent = part
	balloon.Position = part.Position
	balloon.RopeConstraint.Attachment0 = attach
	balloon.LinearVelocity.Enabled = true
	local character = player.Character
	for i,v in pairs(game.Players:GetChildren()) do
		print(i)
		print(v.Name)
		if part.Parent == v then
			print("human")
		end
	end
	wait("10")
	balloon:Destroy()
	attach:Destroy()
end)

so you want to do that if a part’s parent is the character ? do this :


local character = -- Character Here
local part = -- Part Here

if part:GetFirstAncestorWhichIsA("Model") == character then
    -- Script Here
end
1 Like

its saying
GetFirstAncestorWhichIsA is not a valid member of MeshPart “Workspace.hellobeede.LeftUpperArm”

im pretty sure GetFirstAncestorWhichIsA is not a real thing. are you trolling?

this seems to work
if part.Parent == character then print("human") end
but if the part is an accessory then it doesnt work

function IsPartUnderCharacter(part)
  if part and part.Parent ~= workspace then
    if part.Parent == Character then
      return true
    else
      return IsPartUnderCharacter(part.Parent)
    end
  end
  return false
end

nope just search in the internet

local character = -- Character Here
local part = -- Part 

if part:IsDescendantOf(character) then
    -- Script Here
end

You need to check if the part is a parent of the character, not the player like you did here. You can do something similiar:

for i, v in ipairs(game.Players:GetPlayers()) do
    local character = v.Character
    if v.Character:FindFirstChild(part.Name) then
        print("balloon belongs to character")
    end
end

If this doesn’t work then check if part.Parent = character, I haven’t looked deeply into your code so I don’t know what you are doing with this, just my idea.

Also ipairs instead of pairs is faster.