1. Create a local script and place it anywhere you’d like after paste the code below:
local RunSerivce = game:GetService("RunService")
local highlight = game:GetService("ReplicatedStorage"):WaitForChild("Highlight")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local distance = 5 -- max select distance.
RunSerivce.Heartbeat:Connect(function(delta)
if mouse.Target then
local target = mouse.Target
local magnitude = (target.Position - player.Character.HumanoidRootPart.Position).Magnitude
if magnitude < distance then
if target:GetAttribute("IsItem") == true then
highlight.Adornee = target
else
highlight.Adornee = nil
end
else
highlight.Adornee = nil
end
else
highlight.Adornee = nil
end
end)
2. Create a highlight inside ReplicatedStorage
Like so:
3. Get any part/item and add the attribute IsItem
Like so:
Make sure its a boolean and it is set to true like in the screenshot.
If everything is done correctly then you shouldn’t get any errors.
Run the game, walk close to the item in question and hover your mouse over it.
The code is oddly formatted and doesn’t explain the script for developers who don’t understand it, and it doesn’t explain how to apply this in a practical sense in game.
This post reminds me that last time I wanted to code a system like this, highlights didn’t exist. Nicely done, I have no need for this yet but when I do, I’ll probably code it similarly to this because your way’s the same way I’d do it
Something like this should be built-in to existing code, and wouldn’t suffice for a single script from a tutorial. Items could be automatically determined based on their name, tags, or children.
Additonally, you have boilerplate code! This line exists 3 times:
This could be fixed by setting it to nil above the if statements, and only to the target based on the conditions.
local target = mouse.Target
local magnitude = (target.Position - player.Character.HumanoidRootPart.Position).Magnitude
highlight.Adornee = nil
if magnitude < distance then
if target:GetAttribute("IsItem") == true then
highlight.Adornee = target
end
end
And I’m very much sure that that’s your job to explain. If not, this post belongs to #resources:community-resources because it’s more of a setup than a tutorial.
The objective of a tutorial is to explain in detail how to do things and not just giving code and saying to copy-paste it.
Some parts of your code are inconsistent and the code structure is messy which is not a good exemple for beginner scripters, also using a tag instead of attribute make more sense for it.
However, i do not recommend to use this method as it is not an optimized way to do it.
Using ClickDetector is actually a lot better for performances.
Here is the reworked code:
--[[ Roblox Services ]]--
local PlayerService = game:GetService("Players")
local RunSerivce = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--[[ Main Instances ]]--
local Player = PlayerService.LocalPlayer
local Mouse = Player:GetMouse()
local Highlight = Instance.new("Highlight")
--[[ Main Settings ]]--
local Distance = 20
------------------------------------------------------------------------
--[[ Local Functions ]]--
local function CheckTarget()
Highlight.Adornee = nil
if Mouse.Target then
local Target = Mouse.Target
local Magnitude = (Target.Position - Player.Character.HumanoidRootPart.Position).Magnitude
if Magnitude < Distance and Target:HasTag("IsItem") and not (Highlight.Adornee == Target) then
Highlight.Adornee = Target
end
end
end
------------------------------------------------------------------------
--[[ Main Connections ]]--
RunSerivce.Heartbeat:Connect(CheckTarget)
--[[ Highlight Setup ]]--
Highlight.Parent = ReplicatedStorage
Highlight.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
Highlight.FillTransparency = 1
Highlight.OutlineTransparency = 0
Highlight.FillColor = Color3.new(1, 1, 1)
Highlight.OutlineColor = Color3.new(1, 1, 1)
I agree with what @TimeFrenzied said, I know way more than the basics and have been doing this for a while and can indeed tell you that even with the basic this will probably a little bit complicated, not to much but maybe just a tad. Maybe comments in the code will help, just my thoughts.
I see your guys point but for me due to the very simplistic nature of the script I dont feel its needed to really comment on it.
Usually when you’re developing with scripts you should have basic knowledge on language syntax, understanding usages of api’s etc which is why I hadn’t bothered including comments as it wouldn’t really be needed atp.