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

A script to detect movement?

Asked by 11 years ago

Please include the code which you are trying to use, so the community will be better-equipped to help you with your problem.

I wanted a script to detect movement for like a security alarm, I wanted to like put the script in a case and when it detects a person within 4 metres it sets the alarm off, Could someone provide an example script for this?

Thanks,

EliteGamingChicken

3 answers

Log in to vote
0
Answered by
Andalf 100
11 years ago

Best way to do this would be a magnitude check from the camera to the players torso, magnitude is fortunately a property of any vector calculation so your script could look something like this

01local camera = script.Parent
02 
03while true do
04    wait()
05    for k, v in next, game.Players:GetChildren() do
06        local torso = v.Character.Torso
07        if (camera.Position - torso.Position).magnitude < 20 then -- assuming 5 studs is a meter.
08            soundAlarm(); -- a function you can define for the alarm stuff
09        end
10    end
11end

Hope this helps.

Ad
Log in to vote
1
Answered by 11 years ago

I think you should use FindPartOnRay() for that, but OnTouched() will be fine too.

1Main = script.Parent
2 
3function onTouched(part)
4    --do something over there
5end
6 
7Main.Touched:connect(onTouched)

Edit

1local ray = Ray.new(
2        Vector3.new(0, 0, 0), -- Where to start
3        Vector3.new(4, 0, 0) -- How long
4        )
5local part = game.Workspace:FindPartOnRay(ray)
6    if part.Name == "Torso" or "LeftLeg" or "RightLeg" or "Head" or "LeftArm" or "RightArm" then
7    --Do alarm thing there
8end
0
The only problem with that, Panzer is i dont want the thing to be touched I want the find part on ray bit could you change the script to findpartonray() for me please, Thanks EliteGamingChicken 15 — 11y
Log in to vote
0
Answered by
war8989 35
11 years ago

Here, I wrote a simple script for you to use to do that. Modify the variable, dist, to the amount of studs the player must be within to set off the alarm. Also, make sure you point the alarm variable to the alarm.

01alarm = -- point to the alarm
02 
03function findNearestTorso(pos)
04    local list = game.Workspace:children()
05    local torso = nil
06    local temp = nil
07    local dist = 10
08    local human = nil
09    local temp2 = nil
10    for x = 1, #list do
11        temp2 = list[x]
12        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
13            temp = temp2:findFirstChild("Torso")
14            human = temp2:findFirstChild("Humanoid")
15            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
View all 33 lines...

Answer this question