Add code to let enemy NPC(s) check for boolValue(s) in target

Hi
I’m making a horror game that I will not go too in detail about, but one specific mechanic involves checking for a value in players and I need help or a pointer as to how to incorporate this code (the code to check for a boolvalue in the NPC’s target player)

If true, the NPC will be able to attack the player
If false, the NPC will ignore the player/not be able to attack them.

This is the function I aim to place this check in. Something like if boolvalue == true then...

local function canSeeTarget(target)
	local origin = hands.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - hands.HumanoidRootPart.Position).unit * 150
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, hands)
	
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end

I dunno if this kind of check is similar to… say, a kind of kill brick, hope this makes sense

Help is appreciated, cheers

2 Likes

If I understand correctly, probably the first function would be best for this check.
This would let the NPC ignore players it can’t attack, and avoid running your second function.

However, if you wanted the NPC to approach the player but not attack, the second function. But then you may have an issue with switching the NPC to a different target.

1 Like

Not quite sure what you are wanting.
Your post says “let enemy NPC check for boolValue in target”

But then in the text, you ask “which of these functions suit it best”, however the functions have nothing to do with reading a boolValue.

Also in your text you say Function1 checks if the target is seen, and Function 2 moves them to the target, but that isnt correct either.

Function1 does check if the target is non obstructed, but function 2 is just finding the closest player to the enemy with Magnitude.

So not really sure what any of the functions have to do with your original question.

However, I would use Attributes on the Character instance.

nearestTarget:SetAttribute(“AlreadyTargeted”,true)
and
if nearestTarget:GetAttribute(“AlreadyTargeted”) == ture then ‘dont attack’ end

2 Likes

I appreciate the reply, but I’m also asking how I would go about adding this check.
As I mentioned in my post, I’m not 100% sure if it’s similar to something like a killbrick where I check the parent of the object touched or something

1 Like

I thought what I wanted was pretty straightforward. Not sure how you can be unsure of what I’m looking to do.

I appreciate the attributes advice, but it’s not quite what I aim for, and I think you just misunderstood. I’m asking for help on incorporating code into one of these two functions that can check for the boolvalue inside the target.

1 Like

Perhaps I could visualize it if you explain what the bool value will represent, in other words, what is it they are wanting to check for?

That would let me better determine which of the above functions would suit the situation best. As is, they both could check for a bool value on the Character, just not sure which is better.

1 Like

Edited the post to only include one function, too cluttered with both of em.

I was thinking on adding a variable and adding to the if hit parts of the code like so:

-- code up here blabla

local bool = -- idk how to check a target's descendants/children instances
-- which is what I'm asking for help on

if hit:IsDescendantOf(target) and bool.Value == true then
			return true
		end

Or something like this.
All I really want to achieve is to allow the NPC to check if the boolvalue inside their target is true or false, if it’s false then the NPC will not be able to attack the player, and vice versa if it were true

2 Likes

If you just need to check if a part that is hit, contains a bool or not, as I said, use an attribute on the part, such as…

if hit:IsDescendantOf(target) and hit:GetAttribute("BoolValue") == true then
return true
end

This really is the best way to set and check bool values or other types of values on an instance.

1 Like

I wouldn’t really use ValueBases or attributes (slightly better) due to performance reasons, but if you really want to use them, choose attributes over ValueBases. Now with that said, just do player:SetAttribute("CanHit", false) whenever you want the player to be unhittable (lol idk if that’s even a word). Also here’s the modified function, which is also optimized as it won’t do any ray checks if the player can’t be hit, saving resources.

local function canSeeTarget(target)
    if not (target:GetAttribute("CanHit") == true) then
        return false
    end

	local origin = hands.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - hands.HumanoidRootPart.Position).unit * 150
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, hands)
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end
1 Like

this is using attributes, and I’d have to modify all of my code and the client’s code as well to use attributes, which would be even more of a hassle. All I need is to check if the boolvalue is true, not add an attribute or whatever.

same case here @Mystxry12

1 Like

To access the value of a boolValue instance in the player.

if not player.CanBeAttacked.Value then
    return false
end
1 Like

Oh all right just do

local function canSeeTarget(target)
    if not target.CanHit.Value then
        return false
    end

	local origin = hands.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - hands.HumanoidRootPart.Position).unit * 150
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, hands)
	
	if hit then
		if hit:IsDescendantOf(target) then
			return true
		end
	else
		return false
	end
end
1 Like

Not exactly sure what this is gonna do.
As far as I can see it just checks if there’s a value or not at all. I definitely could be wrong and I probably am so correct me if I’m incorrect. I need the code to check if this value Is true or false, not if it just exists.

@Mystxry12 isn’t this just the same??

1 Like

Since .Value returns the value of the bool it will always be true or false; it’s not checking if the bool value exists.

1 Like

there’s no part in the code that checks if it’s true or false, which is why I’m interpreting it as “This is just checking if it exists”
Again, I’m not sure what this is gonna do, it seems a bit unfinished to me

1 Like

Alright so you are thinking something along the lines of:

if target.CanHit.Value == false then
    return
end

This will work yes, but the previous solution I gave does the same too.

1 Like

If we were just checking if the thing exists, the code would be:

if not target.CanHit then ... end

which it isn’t.

FYI, what the current code does is get the value of the bool and if its not true then return false else continue with the function. We do not need to explicitly check if the value == true or value == false because the value is a bool.

Okay, this is something I understand a lot more
Now I just need to add the part that actually allows the code to continue if the value is true, cause all I have right now is to attack regardless, unless this code already prevents the npc from attacking with just those 3 lines

1 Like

If the bool value is false then the function will return false, preventing all the other calculations. The code already does that.

1 Like

Gotcha, speaking of which:

I changed the code slightly to this:

local isInLight = target.HumanoidRootPart:WaitForChild("inLight")
	
	if isInLight.Value == false then
		return
	end

Before I changed it, the output printed that the value was not a valid member.