Objective: Make a script that makes the npc's hitbox part visible when the player has the tool equipped and have their mouse on the hitbox.
Problem: The hitbox turns visible when hovered over but I don't know how to make the hitbox to go back to normal when the tool isn't equipped and when they aren't pointing at the hitbox.
In my game there is a folder in workspace named npcs and inside it are the npcs. Each npc has a part in them named hitbox which is what I'm trying to edit. This is a local script inside a tool's handle.
01 | player = game.Players.LocalPlayer |
02 | handle = script.Parent |
03 | tool = script.Parent.Parent |
04 |
05 |
06 | tool.Equipped:connect( function (mouse) |
07 | while tool:IsDescendantOf(player.Character) do -- Loops while the tool is in the player's character (equipped) |
08 | wait( 0.1 ) |
09 | ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - handle.CFrame.p).unit * 30 ) |
10 | -- Makes the rays |
11 | part = workspace:FindPartOnRay(ray, player.Character, false , true ) |
12 | -- finds the part |
13 | if part then -- checks if it found a part. |
14 | if part.Name = = "Hitbox" and part:IsDescendantOf(workspace.NPCs) then |
15 | -- checks if the part is a hitbox and it is a descendant of the NPC folder. |
Hello, Adv3rtizement!
Use mouse.target
to get the part mouse is pointing to =D, so don't need to use Rays(they're not really precise)
Using Ray(not precise)
01 | player = game.Players.LocalPlayer |
02 | handle = script.Parent |
03 | tool = script.Parent.Parent |
04 |
05 | local ChangedPart = nil |
06 |
07 | tool.Equipped:connect( function (mouse) |
08 | while tool:IsDescendantOf(player.Character) do -- Loops while the tool is in the player's character (equipped) |
09 | wait( 0.1 ) |
10 | ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - handle.CFrame.p).unit * 30 ) |
11 | -- Makes the rays |
12 | part = workspace:FindPartOnRay(ray, player.Character, false , true ) |
13 | -- finds the part |
14 | if part then -- checks if it found a part. |
15 | if ChangedPart then |
Using mouse.target
01 | player = game.Players.LocalPlayer |
02 | handle = script.Parent |
03 | tool = script.Parent.Parent |
04 |
05 | local ChangedPart = nil |
06 |
07 | tool.Equipped:connect( function (mouse) |
08 | while tool:IsDescendantOf(player.Character) do -- Loops while the tool is in the player's character (equipped) |
09 | wait( 0.1 ) |
10 | part = mouse.target -- mouse.target is the part the mouse is pointing to.... |
11 | -- finds the part |
12 | if part then -- checks if it found a part. |
13 | if ChangedPart then |
14 | ChangedPart.Transparency = 1 --Backs the transparency to 1 |
15 | ChangedPart = nil |
Hope this helps
Good Luck with your games