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

Is there a function for distance for dialogs?

Asked by
yoshi8080 445 Moderation Voter
9 years ago

For dialogs, if a player walks away from the conservation distance, the conservation ends, and is there a function for that? for example a sound is in playergui, and if the player isn't in the dialog range then the sound is removed. Is there some function for that?

1 answer

Log in to vote
2
Answered by
Marios2 360 Moderation Voter
9 years ago

The main Dialog instance has a property ConversationDistance which is responsible for allowing the player to chat with an NPC from a restricted distance and interrupt the chat if the distance is too far. So, you are requesting something similar to this - and it's called...

.magnitude

This is a property of Vector3s which is used to get the distance of the vector from the center of the world (0,0,0). Of course, getting this distance tends to not be useful when the other point you want to get the distance from is not 0,0,0.

So, what you do is take both vectors, subtract them (in any order), put them in a parenthesis and add the topic of this answer, .magnitude.

(game.Players.LocalPlayer.Character.Torso.Position - workspace.Part.Position).magnitude

Naturally, this above alone will cause an error, since we aren't doing anything with this like making it into a variable. I put it alone to avoid any confusion - as you see the subtraction is put in a parenthesis, and THEN there is .magnitude. If it wasn't there things would be different:

game.Players.LocalPlayer.Character.Torso.Position - workspace.Part.Position.magnitude

This would subtract the player's distance with the distance of Part from 0,0,0, not the position of Part itself. Thankfully, the order you subtract the vectors in the parenthesis doesn't matter.

This:

(workspace.Part.Position - game.Players.LocalPlayer.Character.Torso.Position).magnitude

Is equal to the first code block shown, result-wise.

This is pretty much it for .magnitude, but one more thing: The result is not a Vector3, but a number.

I hope you've read this all the way, and that it helped you!

EDIT You wanted a way to know when the player goes too far from the dialog to be used for other things other than ending the dialog - unfortunately the Dialog instance doesn't provide any sort of event for that.

Ad

Answer this question