I am wondering how to make a monster that moves/attacks when a player isn't looking at it (Bassicly the opposite of slender man). I have tried this before but the best I got was a script that detected when a mouse entered a click detector in the monster. When this happened it would anchor the parts in the monster making it unable to move. But the downside is it only stops moving when a players mouse hovers over it so the player could still be attacked even though the monster can be seen. Anyone know how to do this properly?
Snipit of the code I used:
Local monster = script.Parent monster.ClickDetector.MouseEnter:Connect (function () monster.HumanoidRootPart.Anchored = true end) monster.ClickDetector.MouseLeave:Connect (function () monster.HumanoidRootPart.Anchored = false end)
Here's a Local Script to detect, if Descendants of a Model is in, player's view.
In the script it tells you where you put the code for when "monster is being seen" or when "monster is not being seen"
Look below the script to get a short explanation of how it works.
Also, if this helps, then, please accept my answer. It would be appreciated. :)
Script:
-- you can set the Model yourself -- read the green. it will tell you where to put your code local monsterCharacterModel = nil -- defining monsterCharacterModel local function noObstructs(position,character) -- defining function local castPoints = {workspace.CurrentCamera.CFrame.Position,position} -- defining castPoints local ignoreList = {game.Players.LocalPlayer.Character, character} -- defining ignoreList local obstructs = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints,ignoreList) -- defining obstructs if obstructs[1] then -- obstruct check -- "not looking at monster" code here elseif not obstructs[1] then -- obstruct check -- "looking at monster" code here end -- close if then statement end -- close function local function ViewPointCheck() -- defining function for i,v in pairs(monsterCharacterModel:GetDescendants()) do if v:IsA"Part" or v:IsA"MeshPart" then -- is part or meshpart check local part,bool = workspace.CurrentCamera:WorldToScreenPoint(v.Position) -- defining WorldToScreenPoint if bool then -- isVisible check noObstructs(v.Position,v.Parent) -- calling function elseif not bool then -- isNotVisible check -- "not looking at monster" code here end -- close isVisible check end -- close is part or meshpart check end -- close for loop end -- close function game:GetService("RunService").Heartbeat:Connect(ViewPointCheck)
The Local Script uses Heartbeat Event to constantly call the function ViewPointCheck. The Function ViewPointCheck checks if every Part and MeshPart of monsterCharacterModel's Position is Visible within the Camera's View. And if Visible is true then it call the function noObstructs. The Function noObstructs checks if there are any parts blocking two points. Which the two points are the Camera and the Visible Part. If there are obstructs then the part is not physically visible. If there are no obstructs then the part is physically visible.