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

Why won't this magnitude script work correctly?

Asked by
Sorukan 240 Moderation Voter
6 years ago
Edited 6 years ago

This is the local script

01local player = game.Players.LocalPlayer
02local char = player.Character
03 
04local part1 = char:WaitForChild("UpperTorso")
05local part2 = game.Workspace.Part
06 
07local distance = (part1.Position - part2.Position).magnitude
08local radius = 10
09 
10if distance <= radius then
11    print("Human Nearby")
12end

It worked fine when i tried printing the magnitude but it doesn't work when i use the if statement even though the output window doesn't show any error.

1 answer

Log in to vote
4
Answered by 6 years ago
Edited 6 years ago

What I think you are trying to do is when the player's UpperTorso gets within the radius of radius which is 10, then print "Human Nearby". Your script runs only once when the game starts and since distance is not <= radius the if doesn't run.

You should try using a while loop for this:

01local player = game.Players.LocalPlayer
02local char = player.Character
03 
04local part1 = char:WaitForChild("UpperTorso")
05local part2 = game.Workspace.Part
06 
07local radius = 10
08 
09while true do
10    local distance = (part1.Position - part2.Position).magnitude
11 
12    if distance <= radius then
13        print("Human Nearby")
14    end
15    wait()
16end

or if you want it to only print once:

01local player = game.Players.LocalPlayer
02local char = player.Character
03 
04local part1 = char:WaitForChild("UpperTorso")
05local part2 = game.Workspace.Part
06 
07local radius = 10
08 
09local debounce = true
10while true do
11    local distance = (part1.Position - part2.Position).magnitude
12 
13    if debounce and distance <= radius then
14        debounce = false
15        print("Human Nearby")
16    else not debounce and distance > radius then
17        debounce = true
18    end
19    wait()
20end

The while loop will keep running and check if distance <= radius instead of only checking once.

I have not tested it so I'm definitely not 100% sure it'll work.

0
Yeah they didn't work Sorukan 240 — 6y
0
sorry now i updated it, i forgot to include the distance but it should work now GoldAngelInDisguise 297 — 6y
0
oh yeah it works now thanks! Sorukan 240 — 6y
Ad

Answer this question