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

How do i get if a player is within 7 studs of a object?

Asked by
funzrey 58
9 years ago

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:

01game.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)
07end)
08function processCommand(speaker, message)
09    -- if someone chatted "whoami"
10    local playerW7s = 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(playerW7s == true) then
13        local msg = Instance.new("Message", game.Workspace)
14 
15        msg.Text = message
16        wait(5)
17        msg:Destroy()
18 
19end
20end

Please help, thanks

4 answers

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
9 years ago

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 
03microphone = --path to the microphone
04distance = 7 --distance in studs for microphone to activate
05 
06function 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
14end
15 
16game.Players.PlayerAdded:connect(function(player)
17    player.Chatted:connect(function(message)
18        processCommand(player, message)
19    end)
20end)
0
In a future, more advanced version of this script you'll want to use a coroutine to create and destroy the message, and you'll want to check that the player has a Torso (i.e. exists in the physical world) before testing for distance. duckwit 1404 — 9y
0
Duckwit, it wont work, I tried typing Whoami WHOami, etc. and it wont say anything. so its not tested funzrey 58 — 9y
0
There was a typographical error. I wrote "=" in the conditional rather than "==", I've fixed it now. It's always assumed on ScriptingHelpers that you can test things for yourself. We're not here to script for you, but to get you set in the right direction. duckwit 1404 — 9y
Ad
Log in to vote
2
Answered by 9 years ago
01game.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)
07end)
08function processCommand(speaker, message)
09    -- if someone chatted "whoami"
10    local playerW7s = 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(playerW7s == true) then
15                 local msg = Instance.new("Message", game.Workspace)
View all 25 lines...
Log in to vote
1
Answered by 9 years ago

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.

1if (player.Character.Torso.Position - workspace.Mic.Position).magnitude <= 7 then
2    --display the message
3    --destroy the message
4end
0
One does not need to constantly refresh the player's position - you can check the position of the player only when they chat. Much more efficient. duckwit 1404 — 9y
0
True, I wrote this in 2 minutes and I was tired so I must've been blanking out FearMeIAmLag 1161 — 9y
Log in to vote
1
Answered by
Hexcede 52
9 years ago
01game.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)
07end)
08function processCommand(speaker, message)
09    if message:lower() == "whoami" then
10            local playerW7s = speaker:DistanceFromCharacter(mic:GetModelCFrame().p)
11            print("Checking if player is within 7 studs..")
12            if(playerW7s == true) then
13                print("The mic is in 7 studs of the speaker.")
14                local msg = Instance.new("Message", workspace)
15                msg.Text = message
View all 21 lines...

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

Answer this question