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:
game.Players.PlayerAdded:connect(function(player) -- when `player` joins the game, start listening for their chatting player.Chatted:connect(function(message) -- and each time they chat, do stuff with `player` and `message` processCommand(player, message) end) end) function processCommand(speaker, message) -- if someone chatted "whoami" local playerW7s = true --i dont know how to get if a player is within 7 studs of a object print("Checking if player is within 7 studs..") if(playerW7s == true) then local msg = Instance.new("Message", game.Workspace) msg.Text = message wait(5) msg:Destroy() end 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.
--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. microphone = --path to the microphone distance = 7 --distance in studs for microphone to activate function processCommand(speaker, message) if message:lower() == "whoami" then --works for "WHOAMI", "WhoAmI", "whoamI", etc.. if (speaker.Character.Torso.Position - microphone.Position).magnitude <= distance then local msg = Instance.new("Message", game.Workspace) msg.Text = message wait(5) msg:Destroy() end end game.Players.PlayerAdded:connect(function(player) player.Chatted:connect(function(message) processCommand(player, message) end) end)
game.Players.PlayerAdded:connect(function(player) -- when `player` joins the game, start listening for their chatting player.Chatted:connect(function(message) -- and each time they chat, do stuff with `player` and `message` processCommand(player, message) end) end) function processCommand(speaker, message) -- if someone chatted "whoami" local playerW7s = false --i dont know how to get if a player is within 7 studs of a object local mag = (part.Position - speaker.Character.Torso.Position).magnitude print("Checking if player is within 7 studs..") if mag <=7 then if(playerW7s == true) then local msg = Instance.new("Message", game.Workspace) msg.Text = message wait(5) msg:Destroy() end end end -- btw your script cleaning is horrendous. -- [[ What .magnitude basically does is find if your user is closer than x studs, by calculating how far point a is from point b. ]] --
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.
if (player.Character.Torso.Position - workspace.Mic.Position).magnitude <= 7 then --display the message --destroy the message end
game.Players.PlayerAdded:connect(function(player) -- when `player` joins the game, start listening for their chatting player.Chatted:connect(function(message) -- and each time they chat, do stuff with `player` and `message` processCommand(player, message) end) end) function processCommand(speaker, message) if message:lower() == "whoami" then local playerW7s = speaker:DistanceFromCharacter(mic:GetModelCFrame().p) print("Checking if player is within 7 studs..") if(playerW7s == true) then print("The mic is in 7 studs of the speaker.") local msg = Instance.new("Message", workspace) msg.Text = message wait(5) msg:Destroy() end end end end
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