So basically I got help with the code to work and then after reviewing it it worked pretty fine and how I intended it to be. But when I run it and hover over a player, Then I move my mouse while in the player it starts to glitch and isn’t how I want it to be. I’m still confused on this as everything I’m doing seems fine but I don’t know what I am doing exactly wrong.
Script:
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RunService = game:GetService('RunService')
RunService.RenderStepped:Connect(function()
if mouse.Target then
if mouse.Target.Parent:FindFirstChild("Humanoid") then
print("Humanoid")
for i, v in pairs(mouse.Target.Parent:FindFirstChild("Humanoid").Parent:GetChildren()) do
if v:IsA("Part") then
if v.Material == Enum.Material.Plastic then
v.Material = Enum.Material.ForceField
v.Color = Color3.fromRGB(255, 0, 0)
else
v.Material = Enum.Material.Plastic
v.Color = Color3.fromRGB(234, 184, 146)
end
end
end
end
end
end)
The problem could be the ForceFiled material, also try to add a cooldown.
Add or v:IsA("MeshPart")
because some of the part that compose a character are MeshParts and not simple Parts.
As I can see in the video you want that this happens only on that NPC but using this code this will happens on every player in the game, to fix this you can insert a value in the NPC and rename it like “NPC” and check in the script if the character that is being hovered has that value into it.
reason its not staying forcefield is because the character is moving thus your mouse keep missing it and get it also avoid using IsA(“Part”) and use IsA(“MeshPart”) as a character has mesh parts
It’s probably because when you hover over an accessory and you try to get the parent, it’ll get the accessory itself, not the character, to fix that, first check if there’s a humanoid in the first parent, and if there isn’t, check the 2nd parent
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RunService = game:GetService('RunService')
RunService.RenderStepped:Connect(function()
if mouse.Target then
local parent = mouse.Target.Parent
local hum = parent:FindFirstChild("Humanoid") or parent.Parent:FindFirstChild("Humanoid")
if hum then
print("Humanoid")
for i, v in pairs(hum.Parent:GetChildren()) do
if v:IsA("Part") then
if v.Material == Enum.Material.Plastic then
v.Material = Enum.Material.ForceField
v.Color = Color3.fromRGB(255, 0, 0)
else
v.Material = Enum.Material.Plastic
v.Color = Color3.fromRGB(234, 184, 146)
end
end
end
end
end
end)
Edit: And I believe another thing could be that when when it loops through the children and it checks the materials, it’ll cause a loop between setting it to forcefield and plastic, you should fix t hat up as well
Well I made a new script or edited my old one, but the effect of it glitching is now gone. But it just doesn’t let me change back to the original color unless I hover above the players head. Aswell asthat, it doesn’t pick up target once hovered on head.
Script:
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local Selection = Instance.new("SelectionBox")
Selection.Visible = true
local Selected = nil
RunService.RenderStepped:Connect(
function()
local target = mouse.Target
if target then
local humanoid = target.Parent:FindFirstChild("Humanoid")
if humanoid then
if Selection.Adornee ~= humanoid.Parent then
Selected = humanoid.Parent
Selection.Parent = humanoid.Parent
Selection.Adornee = humanoid.Parent
for i, v in pairs(humanoid.Parent:GetChildren()) do
if v:IsA("Part") or v:IsA("MesPart") then
v.Material = Enum.Material.ForceField
v.Color = Color3.fromRGB(186, 75, 255)
end
end
end
else
if Selection.Adornee ~= nil then
Selected = nil
Selection.Parent = nil
Selection.Adornee = nil
if mouse.Target then
local parent = mouse.Target.Parent
local hum = parent:FindFirstChild("Humanoid") or parent.Parent:FindFirstChild("Humanoid")
if hum then
for i, v in pairs(hum.Parent:GetChildren()) do
if v:IsA("Part") or v:IsA("MesPart") or v.Color == Color3.fromRGB(186, 75, 255) then
v.Material = Enum.Material.Plastic
v.Color = hum.Parent["Body Colors"].HeadColor3
end
end
end
end
end
end
end
end
)
That isn’t an npc, it’s a playing player…
Check my new code although, I have fixed the glitching with my old solution but it won’t change back to the original players color unless hovered above a specific part.