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
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.
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
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.
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.
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
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
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.
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
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.
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
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