Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make a monster that moves when a player isn't looking at it?

Asked by
Uluomis 32
4 years ago

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)
0
you could use dot products to make a field of sight theking48989987 2147 — 4y
0
Can you elaborate? Uluomis 32 — 4y
0
Is this a multiplayer game? What if multiple players are not seeing it? Who is to be chased? programmerHere 371 — 4y
0
The nearest person would be chased. If one person looks at it then it would stop moving for all clients. I'm making this for both single and multiplayer games. Sorry if i didn't explain enough im on mobile right now Uluomis 32 — 4y

1 answer

Log in to vote
1
Answered by
ryan32t 306 Moderation Voter
4 years ago
Edited 4 years ago

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.

0
Thank you for the answer. It seems advanced but if I practice it I think i'll understand it. I cant accept your answer right now since I'm on mobile and can't test it but if it works ill make sure to except your answer Uluomis 32 — 4y
0
So many comments but so little construction in them.. CeramicTile 847 — 4y
0
I keep geting this error: 16:02:31.901 - Workspace.SmileMonster.Chase:34: attempt to index field 'LocalPlayer' (a nil value) 16:02:31.903 - Stack Begin 16:02:31.904 - Script 'Workspace.SmileMonster.Chase', Line 34 16:02:31.905 - Stack End Uluomis 32 — 4y
0
This has not worked at all for me. Uluomis 32 — 4y
View all comments (5 more)
0
if you have discord, i would gladly help you. mine is ryan32t#6685 ryan32t 306 — 4y
0
i do and i will add you Uluomis 32 — 4y
0
ok ryan32t 306 — 4y
0
Sent u the request Uluomis 32 — 4y
0
i accepted it, i think ryan32t 306 — 4y
Ad

Answer this question