How to make distance for highlight

I get an error saying attempt to index nil with ‘WaitForChild’ in the 6th line

I see, the character must also not be loading in fast enough, to fix this, I would wait for the character as well, this can be done by doing:

local Character = player.Character or player.CharacterAdded:Wait()

You should put this line after you declare the player variable and then change your if statement to this:

if Character.PrimaryPart == nil then return end

May I also ask what the if statement is for?

1 Like

Actually not true. You can use a server script and change where it will run. That’s really handy for the problem described here, and I would recommend using it.

1 Like

So run this on a server script, but with a local/client environment. It should honestly just work, with a chance of needing to wait for the player.

1 Like

What do you mean with a local/client environment?

It’s called RunContext, and you can set it to client. Sorry, I should’ve linked it immediately. Here it is: [Beta] Script RunContext - Updates / Announcements - DevForum | Roblox

1 Like

I changed it to client, and now I get no error. But it still doesn’t work the way I want it to.

What way does it work and what way do you want it to work?

I have a highlight inside a part. And I want that highlight to show up when the player gets at least 6 studs away, and disappear when he gets out of 6 studs. Right now I get no error, but the highlight is still showing up whenever I go.

I do believe the problem is here, does your highlight lose it’s transparency when you get near the part?

No, I don’t even think highlight has a transparency property, I just want it to disable and enable depending on the distance

What’s the behaviour of the highlight right now?

1 Like

Literally just a highlight. The script doesn’t change anything.

If you want a simpler way to do this, I suggest just using a localscript and put it into the starterplayerscripts and use this code:

local player = game.Players.LocalPlayer
local MinDistance = 0  
local MaxDistance = 6  

game:GetService("RunService").Heartbeat:Connect(function()
	if player.Character.PrimaryPart == nil then return end
local character = player.Character or player.CharacterAdded:Wait()
local humanoidP = character.PrimaryPart
local part = game.Workspace["E to Open Door"] -- Put your part
local distance = (humanoidP.Position - part:GetPrimaryPartCFrame()).Magnitude -- Get distance
if distance < MaxDistance and distance > MinDistance then
part.Highlight.Enabled = true
else
part.Highlight.Enabled = false
end
end)

You also need to define part

I get an error saying invalid argument #1 (CFrame expected, got Vector3). Can you create a new place on roblox studio and try it there to see if you get the same error too?

local player = game.Players.LocalPlayer
local MinDistance = 0  
local MaxDistance = 6  

game:GetService("RunService").Heartbeat:Connect(function()
	if player.Character.PrimaryPart == nil then return end
local character = player.Character or player.CharacterAdded:Wait()
local humanoidP = character.PrimaryPart
local part = game.Workspace["E to Open Door"] -- Put your part
local distance = (humanoidP.Position - part:GetPrimaryPartCFrame().Position).Magnitude -- Get distance
if distance < MaxDistance and distance > MinDistance then
part.Highlight.Enabled = true
else
part.Highlight.Enabled = false
end
end)

Sorry, I believe I forgot to change the magnitude to use position

Well it works perfectly fine, but I get this error. attempt to index nil with ‘PrimaryPart’
Should I just ignore it?

What are you currently highlighting? Is it a model or a part?

1 Like

Declare the character variable before the if player.Character.PrimaryPart == nil then return end statement

1 Like

Alright this script is working perfectly fine, thanks!

local player = game.Players.LocalPlayer
local MinDistance = 0  
local MaxDistance = 4.6
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local Character = player.Character or player.CharacterAdded:Wait()


game:GetService("RunService").Heartbeat:Connect(function()
	if Character.PrimaryPart == nil then return end
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidP = character.PrimaryPart
	local part = game.Workspace.Door 
	local distance = (humanoidP.Position - part:GetPrimaryPartCFrame().Position).Magnitude -- Get distance
	if distance < MaxDistance and distance > MinDistance then
		part.Highlight.Enabled = true
	else
		part.Highlight.Enabled = false
	end
end)