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

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

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

4 answers

✓ Accepted answer

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)

3 comments

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.

0

Duckwit, it wont work, I tried typing Whoami WHOami, etc. and it wont say anything. so its not tested

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.

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

2 comments

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.

0

True, I wrote this in 2 minutes and I was tired so I must've been blanking out

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

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