Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't this magnitude script work correctly?

This is the local script

local player = game.Players.LocalPlayer
local char = player.Character

local part1 = char:WaitForChild("UpperTorso")
local part2 = game.Workspace.Part

local distance = (part1.Position - part2.Position).magnitude
local radius = 10

if distance <= radius then
	print("Human Nearby")
end

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

✓ Accepted answer

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:

local player = game.Players.LocalPlayer
local char = player.Character

local part1 = char:WaitForChild("UpperTorso")
local part2 = game.Workspace.Part

local radius = 10

while true do
    local distance = (part1.Position - part2.Position).magnitude

    if distance <= radius then
        print("Human Nearby")
    end
    wait()
end

or if you want it to only print once:

local player = game.Players.LocalPlayer
local char = player.Character

local part1 = char:WaitForChild("UpperTorso")
local part2 = game.Workspace.Part

local radius = 10

local debounce = true
while true do
    local distance = (part1.Position - part2.Position).magnitude
    
    if debounce and distance <= radius then
        debounce = false
        print("Human Nearby")
    else not debounce and distance > radius then
        debounce = true
    end
    wait()
end

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.

3 comments

0

Yeah they didn't work

0

sorry now i updated it, i forgot to include the distance but it should work now

0

oh yeah it works now thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.