Make an NPC carry the player

I have an issue where the script inside another NPC cannot find the HRP of the closest player it’s targetting.

I want my enemy to weld the player to the grab-hitbox on the RightArm of the enemy’s rig. I don’t know how to script the NPC to find the closest’s players torso in a casual script, not a localscript. The plot is to make the enemy grab the player by the grab-hitbox on the right arm, in which the player is unavailable to move by being gripped by the weld.

Here is the best example on youtube, by it’s first few seconds in as the giant noob grabs the player smoothly.

1 Like

finding the closest player is simply just using 2 position to divide and convert it to magnitude, then checking if it’s below the distance given to the argument, so with that being said when you script it based on the description, it will turn out like this.

function find_tar(range) --arg of this just uses a number
  local tar = nil 
  local range2 = range or 10000 --if left blank, your range by default is set to 10000
  for _, plyr in pairs(playerSV:GetPlayers()) do --loop through the players connected in the game 
      local char = plyr.Character --define character from a player
      local min_dist = (enemy.PrimaryPart.Position - char.PrimaryPart.Position).Magnitude --enemy is the one you're attaching the script to, Compare it with PrimaryPart position, NOTE: if enemy does not have PrimaryPart, set them to HumanoidRootPart otherwise an error is given
      if min_dist < range2 then --optionally you can add condition if player's health is 0 so it won't grab the corpses
          range2 = min_dist
          tar = char
      end
  end
  return tar, range2 --returns tar and closest distance, once returned, using this in another function or outside will give you the target character
end

and for grabbing, it’s simply just using the weld or Motor6D to connect player character’s root part to the hand, you just create it upon grabbing state and weld it through the script, just make sure to set the C0 or C1 for that, it’s best to use a dummy character to adjust the position and orientation for that.

Can we see your script so far? If you’re using a hitbox then you can just find the Player’s HumanoidRootPart from there?

1 Like

for hitbox itself, i would just use some zone checking by using Touched event on the hand but if your NPC is struggling to grab the player then you can make another function for that aswell, since i assume the character is going to be gigantic, I also recommend setting the range argument higher for it to catch.

probably i can make an example one like in many post where i gave out a place to mess around with but rn writing this on mobile is extremely unreliable zzzzzzzzz

Sorry, I meant to ask OP what they were using right now. Personally I would use Raycast Hitboxes, or Muchacho’s Hitbox because i don’t use zone plus or spatial queries often.

1 Like

i dont entirely know where I’d place this script, inside the hitbox part or inside the model’s AI?

I would highly recommend keeping this script in a module so you can easily access it across many scripts and use it for a host of different enemies and whatever else you’d need it for.

1 Like