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
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
01 | local camera = script.Parent |
02 |
03 | while 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 |
11 | end |
Hope this helps.
I think you should use FindPartOnRay() for that, but OnTouched() will be fine too.
1 | Main = script.Parent |
2 |
3 | function onTouched(part) |
4 | --do something over there |
5 | end |
6 |
7 | Main.Touched:connect(onTouched) |
Edit
1 | local ray = Ray.new( |
2 | Vector 3. new( 0 , 0 , 0 ), -- Where to start |
3 | Vector 3. new( 4 , 0 , 0 ) -- How long |
4 | ) |
5 | local 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 |
8 | end |
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.
01 | alarm = -- point to the alarm |
02 |
03 | function 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 temp 2 = nil |
10 | for x = 1 , #list do |
11 | temp 2 = list [ x ] |
12 | if (temp 2. className = = "Model" ) and (temp 2 ~ = script.Parent) then |
13 | temp = temp 2 :findFirstChild( "Torso" ) |
14 | human = temp 2 :findFirstChild( "Humanoid" ) |
15 | if (temp ~ = nil ) and (human ~ = nil ) and (human.Health > 0 ) then |