How to get the player Character in an NPC script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I was working on an NPC that spawns and shoots a player when near, but I couldn’t get the Player and Character on the script, which is what I’m trying to achieve so far.

  2. What is the issue? Include screenshots / videos if possible!
    The Shooting mechanic only works on ONE npc. Any other spawn/clone of the same NPC to workspace wouldn’t shoot (the main issue is because I couldn’t find a way to get the Player for its shooting radius which led me to only getting the player when they join).

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?

I couldn’t find any solutions, and yes I have tried looking for solutions

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you! Here is the script attached below (located in ServerScriptService)

local range = 40
local beam = game.ReplicatedStorage.Assets.NPCBeam
local part = game.Workspace.ExploiderSpawner

local ts = game:GetService("TweenService")

local Players = game:GetService("Players")


Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local bot = workspace.Exploiders:WaitForChild("Exploider")
		local bothrp = bot:WaitForChild("HumanoidRootPart")
		local beaming = bot:WaitForChild("Tablet").Beaming.Value
		local commandtext = bot:WaitForChild("Tablet").CommandPrompt.Command
		
		print(Character.Name)
		while task.wait(0.5) do
			if Character:FindFirstChild("Humanoid").Health > 0 then
				local Distance = Player:DistanceFromCharacter(bothrp.Position)

				if (Distance <= range) and bot:FindFirstChild("Humanoid").Health > 0 then
				end
			end
		end
	end)
end)

Thank you for anyone trying to help.

I think its because the players character is only added once, that’s why it only works on one npc. I recommend trying to put a variable for the character, for example: local range = 40
local beam = game.ReplicatedStorage.Assets.NPCBeam
local part = game.Workspace.ExploiderSpawner

local ts = game:GetService(“TweenService”)

local Players = game:GetService(“Players”)
local char – Put a variable here so the functions can refrence it (I abriviate character, but you can change it)

Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local bot = workspace.Exploiders:WaitForChild(“Exploider”)
local bothrp = bot:WaitForChild(“HumanoidRootPart”)
local beaming = bot:WaitForChild(“Tablet”).Beaming.Value
local commandtext = bot:WaitForChild(“Tablet”).CommandPrompt.Command
char = Character – Asign the player character to char

	print(Character.Name)
	while task.wait(0.5) do
		if Character:FindFirstChild("Humanoid").Health > 0 then
			local Distance = Player:DistanceFromCharacter(bothrp.Position)

			if (Distance <= range) and bot:FindFirstChild("Humanoid").Health > 0 then
			end
		end
	end
end)

end)
I dont know how you do death, if it like ends the game or is just normal roblox respawining, but just note when the player dies they will lose there target. I also dont know if its singleplayer or multiplayer

You can get the players through the FindFirstChild Humanoid when in range, but loop through the player names to verify the difference btw a player and an NPC

Yes that’s the problem I’m having, finding a way to get the character in a different way

Thanks for the help. I can do the “looping through the player names to check if its an NPC”, but how would I go about to achieve getting the Humanoid/Character through FindFirstChild? Can you please give an example of how it can be done?

You can use magnitude or getpartsinpart()
Research both and pick whichever to get the Humanoid in range

To differentiate a Certain NPC or a Player’s Character you can use

Players:GetPlayerFromCharacter()

If it returns a Player then it is a Character otherwise it isn’t a Player’s Character.

getpartsinpart() worked very well, thank you!

1 Like

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