I cant get this person hightling script to work

I need to get this script fixed, it’s meant to control a highlight around a person when I take out a thermal imaging camera.

I have parts of it working, but when I added the functions for equip and unequip it broke.

wait(1)
script.Parent.Equipped:Connect(function()	
for i,v in pairs(game.Workspace:GetChildren()) do
	if v.ClassName == "Model" then
		if not v:FindFirstChild('HumanoidRootPart') then
			print('not a player') 	
		else
			local glow = Instance.new("Highlight")
			glow.Parent = v
			
			script.Parent.Unequipped:Connect(function()
					glow:Remove()
	end)
end

Tell me what’s wrong please so I can get it functioning

Thanks :slight_smile:

It might be because you never disconnect the connections you make to the Unequipped event. So each time equipped runs a new function is being connected meaning the amount of times Remove() is called per equip increases with each equip. To fix just do this

local unequipConnection
unequipConnection = script.Parent.Unequipped:Connect(function()
    glow:Remove()
    unequipConnection:Disconnect()
end)

You’re missing about 3 ends… that’s probably the issue.

Fixed code:


wait(1)

script.Parent.Equipped:Connect(function()	

for i,v in pairs(game.Workspace:GetChildren()) do
	if v.ClassName == "Model" then
		if not v:FindFirstChild('HumanoidRootPart') then
			print('not a player') 	
		else
			local glow = Instance.new("Highlight")
			glow.Parent = v
			
			script.Parent.Unequipped:Connect(function()
					glow:Remove()
            end)
        end
	end
end

end)

2 Likes

It still makes my ends incorrect if I’m placing it correctly

You still haven’t placed your ends correctly, I have placed a correct version above for you. :slightly_smiling_face:

Your last end is giving an error

Yeah, sorry… I’m on my phone. I’ve edited it again.

Thank you, seems to be working
:slight_smile:

1 Like