This monster is chasing players on a map but it can be distracted by a distract provided that he can see it. So I used raycasting and casted a ray from monster to distract to see if the way is clear. I also made a blacklist to prevent ray hitting monster’s body parts but when I’m trying to print raycastResult.Instance it says nil or Torso (sometimes Distract when it’s directly looking at it but that’s not how I want it to be), here’s the script:
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {monster} -- `monster` is the monster model shown above
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
local rayOrgin = monster.HumanoidRootPart.Position
local rayDestination = distract.Position
local rayDirection = rayDestination - rayOrgin
local raycastResults = workspace:Raycast(rayOrgin, rayDirection, rayParams)
--[[local raycastResults2 = workspace:Raycast(rayOrgin, rayDirection)
if not raycastResults and raycastResults2 then
print(raycastResults.Instance)
print(raycastResults2.Instance)
end]]
if raycastResults and raycastResults.Instance == distract then
local direction = (distract.Position - monster.HumanoidRootPart.Position).Unit
return distract.Position - (direction * ((tonumber(monster.Size.Value)+1) * 2))
end
Not really sure this is the issue, but use CFrame.Position for both the distract and the monster’s positions, for most moving models, CFrame is correct and not Position.
local rayOrgin = monster.HumanoidRootPart.CFrame.Position
local rayDestination = distract.CFrame.Position
Also, try multiplying the rayDirection by something like 1.05 just to make it go past the distract as an extra layer of assurance to it.
local raycastResults = workspace:Raycast(rayOrgin, rayDirection*1.05, rayParams)
It looks like you’ve done the raycasting correctly, which just means that maybe the monster variable is either not constructed correctly or its just nil, so you could show us how you’ve constructed your “monster” variable?
There’s an ObjectValue in ReplicatedStorage which stores monster model when the game starts and it’s impossible that durning a game it suddenly becomes nil and back a monster
local ReplicatedStorage = game:GetService("ReplicatedStorage")
...
local monsterContainer = ReplicatedStorage:WaitForChild("Monster")
...
while active.Value and monsterContainer.Value:FindFirstChild("HumanoidRootPart") do
local monster = monsterContainer.Value
...
end
Well try using a different approach, since the monster is named “Monster” in workspace just check if the workspace has that:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
...
local monsterContainer = ReplicatedStorage:WaitForChild("Monster")
...
while active.Value and workspace:FindFirstChild("Monster") do
local monster = workspace.Monster
...
end
Without testing I am sure that it won’t change anything and with this approach the other scripts would die because all are using that ObjectValue. The monster name is also not “Monster” but 0, 1, 2, 3, 4 depending on its size, I changed it to “Monster” for better understanding