I want to use highlight to a part but the problem is that I want it to be seen from a specific number of studs. So if you are like for example, 5 studs away from the part, then you cant see the highlight around it.
I tried searching on devforum for a similar post but I couldn’t find anything.
Nobody has ever tried to this before, that’s why you couldn’t find anything related to this. But logically you should research a bunch of different topics that would help you do this.
These things will help you make this:
game:GetService("RunService").RenderStepped (camera.CFrame.Position - part.Position).Magnitude Highlight.Enabled or Highlight.Parent
You would need to use all of these to make the script you want.
Renderstepped will run every frame, which means that you will check on every frame the distance of the highlights and disable it or enable it based on the magnitude of the object.
game:GetService("RunService").RenderStepped -- Event Function for running every frame
(camera.CFrame.Position - part.Position).Magnitude -- Shows the distance between the camera and a part
Highlight.Enabled or Highlight.Parent -- Enables or disables the highlight based on the parent or enable property
I tried to make a functional script and I ended up with this :
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
part.Highlight.Enabled = math.clamp(distance, MinDistance, MaxDistance) / MaxDistance
end)
But I get this error : attempt to index nil with ‘Character’
This is a server script inside the model I want to hightlight. The description is kinda miss-leading because I changed from part to model. Also the script is obviously from another post with the same subject.
You are not able to get the localplayer with a server script, and if you change it to highlight with a server script everybody will see it so I suggest you use a local script
Edit: Sorry, forgot to mention that the local script can only be in some places, it cannot work unless it is parented to the character, playerscripts folder or the startergui.
This past days I’ve been trying to get it to work. And now I have this
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.Door
local distance = (humanoidP.Position - part:GetModelCFrame()).Magnitude
part.Highlight.Enabled = math.clamp(distance, MinDistance, MaxDistance) / MaxDistance
end)
I keep getting this error that says : invalid argument #1 (CFrame expected, got Vector3)
You’re gettting the cframe instead of the position for the distance, I suggest doing cframe.position
The problem is on this line:
local distance = (humanoidP.Position - part:GetModelCFrame()).Magnitude
I suggest doing this:
local distance = (humanoidP.Position - part:GetModelCFrame().Position).Magnitude
The reason why it doesn’t work is because it expected a position, but you gave the cframe, cframe is orientation and position mixed together, but when using magnitude it expects 2 positions.
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.