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

Why is it not detecting distance?

A simple version of my script (provided by ShawnyG in my last question).

Why does this not work:

torso = game.Players.LocalPlayer.Character.Torso
part2 = game.Workspace.Part2

if (torso.Position - part2.Position).magnitude >= 40) then
    print('It is 40 studs away!!!')
end

The script is enabled. There is a LocalScript in the StarterGui. There is a part named "Part2" in the Workspace. There is no output. I'm stumped.

**UPDATE: **I added print("The script is responding") at line 3, but nothing even prints. I'm even more confused now. This is in a LocalScript in the Workspace.

2 answers

✓ Accepted answer

You have an unmatched parenthesis:

if (torso.Position - part2.Position).magnitude >= 40) then
--                                                  ^

Remove it.

This is why nothing printed even at the beginning-- Lua cannot do anything at all if you have a syntax error.

1 comment

0

Ah, my problem was instead of removing that parenthesis, I had added a second one behind it (in an attempt to close the other one pointing to the right). Thank you very much, big problems like these always seem to be caused by such a small problem.

From the wiki: "In order for a LocalScript to function, it must be placed within one of three areas:

-In a player's Backpack (can be indirectly, through a Tool or HopperBin) -In a player's character model -In a player's PlayerGui"

LocalScripts do not run in the workspace, move it to an appropriate place for it to run

2 comments

0

Studio crashes whenever I put it in the player though.

0

There's no reason studio should be crashing. Is this the whole script? Make sure if this is inside a while loop to include a wait(), that is most likely your problem.

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