Hi, I am back, sorry about inactivity, Today i have a question about how do i get if a player is within 7 studs of a object, I am making a microphone, and therefore i dont want everybody that chats putting a message on the screen. The script is inside the actual microphone object, Here is my code:
01 | game.Players.PlayerAdded:connect( function (player) |
02 | -- when `player` joins the game, start listening for their chatting |
03 | player.Chatted:connect( function (message) |
04 | -- and each time they chat, do stuff with `player` and `message` |
05 | processCommand(player, message) |
06 | end ) |
07 | end ) |
08 | function processCommand(speaker, message) |
09 | -- if someone chatted "whoami" |
10 | local playerW 7 s = true --i dont know how to get if a player is within 7 studs of a object |
11 | print ( "Checking if player is within 7 studs.." ) |
12 | if (playerW 7 s = = true ) then |
13 | local msg = Instance.new( "Message" , game.Workspace) |
14 |
15 | msg.Text = message |
16 | wait( 5 ) |
17 | msg:Destroy() |
18 |
19 | end |
20 | end |
Please help, thanks
That's a pretty cool idea, FunzreyAlt!
FearMeIAmLag, yourchicken, and crazycittycat have all addressed the key issue, but in addition to their collective advice, I'll offer some further refinements.
01 | --Personally, I think you should define functions that will be used later in the script before rather than after the block that they are used in. |
02 |
03 | microphone = --path to the microphone |
04 | distance = 7 --distance in studs for microphone to activate |
05 |
06 | function processCommand(speaker, message) |
07 | if message:lower() = = "whoami" then --works for "WHOAMI", "WhoAmI", "whoamI", etc.. |
08 | if (speaker.Character.Torso.Position - microphone.Position).magnitude < = distance then |
09 | local msg = Instance.new( "Message" , game.Workspace) |
10 | msg.Text = message |
11 | wait( 5 ) |
12 | msg:Destroy() |
13 | end |
14 | end |
15 |
16 | game.Players.PlayerAdded:connect( function (player) |
17 | player.Chatted:connect( function (message) |
18 | processCommand(player, message) |
19 | end ) |
20 | end ) |
01 | game.Players.PlayerAdded:connect( function (player) |
02 | -- when `player` joins the game, start listening for their chatting |
03 | player.Chatted:connect( function (message) |
04 | -- and each time they chat, do stuff with `player` and `message` |
05 | processCommand(player, message) |
06 | end ) |
07 | end ) |
08 | function processCommand(speaker, message) |
09 | -- if someone chatted "whoami" |
10 | local playerW 7 s = false --i dont know how to get if a player is within 7 studs of a object |
11 | local mag = (part.Position - speaker.Character.Torso.Position).magnitude |
12 | print ( "Checking if player is within 7 studs.." ) |
13 | if mag < = 7 then |
14 | if (playerW 7 s = = true ) then |
15 | local msg = Instance.new( "Message" , game.Workspace) |
You could use magnitude
to check the distance of the player's torso from the microphone. Of course you will have to keep looping over the player whenever they move in order to update their position and you will have to change the paths to the mic and player objects as this is just an example.
1 | if (player.Character.Torso.Position - workspace.Mic.Position).magnitude < = 7 then |
2 | --display the message |
3 | --destroy the message |
4 | end |
01 | game.Players.PlayerAdded:connect( function (player) |
02 | -- when `player` joins the game, start listening for their chatting |
03 | player.Chatted:connect( function (message) |
04 | -- and each time they chat, do stuff with `player` and `message` |
05 | processCommand(player, message) |
06 | end ) |
07 | end ) |
08 | function processCommand(speaker, message) |
09 | if message:lower() = = "whoami" then |
10 | local playerW 7 s = speaker:DistanceFromCharacter(mic:GetModelCFrame().p) |
11 | print ( "Checking if player is within 7 studs.." ) |
12 | if (playerW 7 s = = true ) then |
13 | print ( "The mic is in 7 studs of the speaker." ) |
14 | local msg = Instance.new( "Message" , workspace) |
15 | msg.Text = message |
I suggest using tabs. Its much cleaner and easier to edit. This was fully tested by me and one of my friends. It should work perfectly and they should see the message if the speaker is in 7 studs of the microphone. If your microphone is not a model then change the mic:GetModelCFrame().p to mic.Position