How to detect if player is near?

So basically, I am making it if the player is near the dummy, the dummy would chat the following dialog. But it doesn’t work.

There is an error, Workspace.Dummy.Script:5: attempt to index nil with 'DistanceFromCharacter'

local player = game.Players.LocalPlayer
local MAX_RANGE = 15
local chat = game:GetService("Chat")

while player:DistanceFromCharacter(workspace.Dummy.Head.Position) < MAX_RANGE do
	chat:Chat(script.Parent.Head,"Welcome to Sabway!")
end
3 Likes
  1. When you’re using While loops, dont forget to put a delay - task.wait()

2)You can calculate the distance/magnitude between the dummy’s torso and the player’s char torso.

3 Likes

Is this Script or Local Script? I don’t think you can use LocalPlayer from server which I think should be nil.

Where is that script located in?

1 Like

I just changed it to a Local Script. Now, there are no errors, but it doesn’t work.

1 Like

Located inside the Dummy. char limit

1 Like

I don’t even know what’s player:DistanceFromCharacter(), looks like a Magnitude checking but it’s player version.

Maybe try using magnitude or distance on Valkyrop’s Post?

2 Likes
local chat = game:GetService("Chat")
local players = game:GetService("Players")
local player = players.LocalPlayer

local dummy = workspace:WaitForChild("Dummy")
local hrp = dummy:WaitForChild("HumanoidRootPart")
local head = dummy:WaitForChild("Head")

while true do
	task.wait(1)
	local distance = player:DistanceFromCharacter(hrp.Position)
	if distance <= 10 then
		chat:Chat(head, "Hello world!")
	end
end

Posting here too since you made two threads.

10 Likes