Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make parts turn visible/invisible when the player is holding a tool and hovering over it?

Asked by 6 years ago

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.

01player = game.Players.LocalPlayer
02handle = script.Parent
03tool = script.Parent.Parent
04 
05 
06tool.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.
View all 25 lines...

1 answer

Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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)

01player = game.Players.LocalPlayer
02handle = script.Parent
03tool = script.Parent.Parent
04 
05local ChangedPart = nil
06 
07tool.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
View all 45 lines...

Using mouse.target

01player = game.Players.LocalPlayer
02handle = script.Parent
03tool = script.Parent.Parent
04 
05local ChangedPart = nil
06 
07tool.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
View all 43 lines...

Hope this helps

Good Luck with your games

0
Thanks Adv3rtizement 101 — 6y
0
Is there a way to make it so it only works within a certain distance? Adv3rtizement 101 — 6y
0
yes, add me on roblox and I make it for you Leamir 3138 — 6y
Ad

Answer this question