Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 answer

✓ Accepted answer

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

3 comments

0

Thanks

0

Is there a way to make it so it only works within a certain distance?

0

yes, add me on roblox and I make it for you

This discussion is preserved as it was. New answers, comments, and votes are closed.