Make your game think an NPC is a player

I am making a game about Vampires and the only way to kill a player character in the game is to get their health low enough and then stake them (with a specific keyboard bind). In my code, before doing damage, a tool or skill checks to see if it is attacking a player by checking if player is nil. If it is nil, it will allow the attack to kill them, but if it returns a player value, then it will only reduce their HP to 1 and stun them. Later in the game, I decided I would like to have NPC vampires that also must be killed in this aspect. Instead of changing the script for all weapons and skills, could I create a model and parent it to the game.Players folder with the name of the NPC so it returns a value when checking for a player?

Could I also setup player-like leaderstats for these NPCs like players have?

I also wouldn’t want to mess up anything like player server count or experience any other unforeseen bugs.

Here is an example of the player check:

function DealDamage(Target,Damage) 
	if Target:FindFirstChild("Humanoid") ~= nil then
		local player = game.Players:GetPlayerFromCharacter(PlayerCharacter)
		Damage = Damage + math.floor((Damage*(player.leaderstats.Str.Value/100)))
		local plr = game.Players:GetPlayerFromCharacter(Target)  -- here is where I check for a player value
		if plr ~= nil then  --and here is where it deviates and checks for player resistance
			if plr.TeamColor ~= player.TeamColor then
				Damage = Damage * (0.99^(plr.leaderstats.Res.Value/100))
				plr.leaderstats.Res.Value = plr.leaderstats.Res.Value + 1
				player.leaderstats.Str.Value = player.leaderstats.Str.Value + 1
				if Damage >= Target.Humanoid.Health then
					Target.Humanoid.Health = 1
					Target.Humanoid:UnequipTools()
					Target.Humanoid.Sit = true
				else
					Target.Humanoid:TakeDamage(math.floor(Damage))
				end
			end	
		else
			player.leaderstats.Str.Value = player.leaderstats.Str.Value + 1
			if Damage >= Target.Humanoid.Health then	
				Target.Humanoid:TakeDamage(math.floor(Damage))
				Target.Head.face.Texture = deadface[math.random(1,#deadface)]
			else
				Target.Humanoid:TakeDamage(math.floor(Damage))
			end	
		end
...

Unfortunately you cannot an players, or a CPU player, directly into the “Players” service. However, you could store your NPC’s in a folder, and then check to see if your Target is in that folder.

For instance:

local plr = game.Players:GetPlayerFromCharacter(Target) 
if plr == nil then
plr = Workspace.CPUfolder:FindFirstChild(Target)
end
if plr ~= nil then  
     --run the rest of your attack script
end

Hope this helps!

1 Like

That should work fine. Thank you.
I was actually more worried about it working, but then messing up a bunch of other things which is probably why it is not doable.

1 Like