Making this work locally only

I made this script that creates click detectors and highlights in every player and allows people to hover over people’s limbs and heads creating a cool effect.

My original goal with this was for it to be a local script, and for it to only create the effect when the local player hovers their mouse over their own character, but for some reason the objects were created, but it seems that the click detector couldn’t be interacted with (which is also the case with this server script).

How could I achieve this effect, where a player can hover their mouse over their own character to highlight it?

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		for _, parts in character:GetChildren() do
			if parts:IsA("BasePart") and string.match(parts.Name, "Leg") or string.match(parts.Name, "Arm") or string.match(parts.Name, "Head") then
				local CD = Instance.new("ClickDetector")
				CD.Parent = parts

				local HL = Instance.new("Highlight")
				HL.Parent = parts
				HL.Enabled = false

				CD.MouseHoverEnter:Connect(function()
					for _, highlights in pairs(character:GetDescendants()) do
						if highlights:IsA("Highlight") then
							highlights.Enabled = true
						end
					end
				end)

				CD.MouseHoverLeave:Connect(function()
					for _, highlights in pairs(character:GetDescendants()) do
						if highlights:IsA("Highlight") then
							highlights.Enabled = false
						end
					end
				end)
			end
		end
	end)
end)

You could just create the same script but without the playeradded event or the characteradded event. Reference the character by using script.Parent in a localscript inside startercharacterscripts.

Roblox has a pretty tight limit on the number of Highlight instances you can have rendering at once. I wouldn’t recommend placing individual ones in each body part of a player’s character. Instead, place a single one in the character itself, as it will then cover the entire assembly alone

I’ve already tried that but it seems that ClickDetectors can’t be used by the local player if they’re inside their character. Thats what I need a solution for.

I’m not looking to have the torso highlighted though. Just the legs, arms, and head

I can get everything to work but the hover or click or even knowing there is a click detector in the body part Major block… Maybe consider a gui interface that will allow you a “workable click” to trigger whatever part.

There’s an existing topic where someone had this problem.

Bind mouse movement to a handler function that checks if the mouse is hovering a limb, then create a highlight for them that’s deleted when the same handler stops hovering over a limb.

This is why forum scripting is so sweet… Good thinking!
I tried somthing like that and used raycasting.

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

player.CharacterAdded:Connect(function(character)
	character:WaitForChild("HumanoidRootPart")
	
	local highlights = {}
	for _, part in ipairs(character:GetChildren()) do
		if part:IsA("BasePart") and (part.Name:match("Left") or part.Name:match("Right") or part.Name == "Head") then
			local highlight = Instance.new("Highlight")
			highlight.Parent = part
			highlight.Enabled = false
			table.insert(highlights, highlight)
		end
	end

	UserInputService.InputChanged:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseMovement then
			local mousePosition = UserInputService:GetMouseLocation()
			local rayOrigin = camera.CFrame.Position
			local rayDirection = (camera:ViewportPointToRay(mousePosition.X, mousePosition.Y).Direction) * 1000

			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {character}
			raycastParams.FilterType = Enum.RaycastFilterType.Include

			local result = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

			for _, highlight in ipairs(highlights) do
				highlight.Enabled = result and result.Instance == highlight.Parent
			end
		end
	end)
end)

Tested and working on an R15 body. I did use Match so it may work on both body types.

2 Likes

I’ll have to do some modifications but this works. Thank you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.