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.
player = game.Players.LocalPlayer handle = script.Parent tool = script.Parent.Parent tool.Equipped:connect(function(mouse) while tool:IsDescendantOf(player.Character) do -- Loops while the tool is in the player's character (equipped) wait(0.1) ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - handle.CFrame.p).unit * 30) -- Makes the rays part = workspace:FindPartOnRay(ray, player.Character, false, true) -- finds the part if part then -- checks if it found a part. if part.Name == "Hitbox" and part:IsDescendantOf(workspace.NPCs) then -- checks if the part is a hitbox and it is a descendant of the NPC folder. part.Transparency = 0 -- Changes part transparency else print("Part Not Hitbox") -- If the part isn't a npc's hitbox end else print("Not part") -- If the ray didn't get a part. end end end)
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)
player = game.Players.LocalPlayer handle = script.Parent tool = script.Parent.Parent local ChangedPart = nil tool.Equipped:connect(function(mouse) while tool:IsDescendantOf(player.Character) do -- Loops while the tool is in the player's character (equipped) wait(0.1) ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - handle.CFrame.p).unit * 30) -- Makes the rays part = workspace:FindPartOnRay(ray, player.Character, false, true) -- finds the part if part then -- checks if it found a part. if ChangedPart then ChangedPart.Transparency = 1 --Backs the transparency to 1 ChangedPart = nil end if part.Name == "Hitbox" and part:IsDescendantOf(workspace.NPCs) then -- checks if the part is a hitbox and it is a descendant of the NPC folder. ChangedPart = part --Stores the part it's changing.... part.Transparency = 0 -- Changes part transparency else print("Part Not Hitbox") -- If the part isn't a npc's hitbox end else if ChangedPart then --Tests if had a part ChangedPart.Transparency = 1 --Backs the transparency to 1 ChangedPart = nil end print("Not part") -- If the ray didn't get a part. end end end) tool.Unequipped:connect(function(mouse) if ChangedPart then wait(.1) ChangedPart.Transparency = 1 ChangedPart = nil end end)
Using mouse.target
player = game.Players.LocalPlayer handle = script.Parent tool = script.Parent.Parent local ChangedPart = nil tool.Equipped:connect(function(mouse) while tool:IsDescendantOf(player.Character) do -- Loops while the tool is in the player's character (equipped) wait(0.1) part = mouse.target -- mouse.target is the part the mouse is pointing to.... -- finds the part if part then -- checks if it found a part. if ChangedPart then ChangedPart.Transparency = 1 --Backs the transparency to 1 ChangedPart = nil end if part.Name == "Hitbox" and part:IsDescendantOf(workspace.NPCs) then -- checks if the part is a hitbox and it is a descendant of the NPC folder. ChangedPart = part --Stores the part it's changing.... part.Transparency = 0 -- Changes part transparency else print("Part Not Hitbox") -- If the part isn't a npc's hitbox end else if ChangedPart then --Tests if had a part ChangedPart.Transparency = 1 --Backs the transparency to 1 ChangedPart = nil end print("Not part") -- If the ray didn't get a part. end end end) tool.Unequipped:connect(function(mouse) wait(.1) if ChangedPart then ChangedPart.Transparency = 1 ChangedPart = nil end end)
Hope this helps
Good Luck with your games