Trouble with player-mouse "view" script

I am trying to make a script that highlights a player if the localplayer is “hovering over” or “viewing” them. (with their mouse of course.)

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

RunService.RenderStepped:Connect(function()
	local Target = Mouse.Target
	if Target and Target.Parent:FindFirstChild("Humanoid") then
		if Players:GetPlayerFromCharacter(Target.Parent) then
			FoundPlayer = Players:GetPlayerFromCharacter(Target.Parent)
			FoundPlayer.Character.DetailHighlight.Enabled = true
			FoundPlayer.Character.Head.Nametag.BasicName.Visible = false
			FoundPlayer.Character.Head.Nametag.Advanced.Visible = true
			FoundPlayer.Character.Head.Nametag.Advanced.Exp.lit.req.Text = FoundPlayer.lvl.Value
		else
			if FoundPlayer then
				FoundPlayer.Character.DetailHighlight.Enabled = false
				FoundPlayer.Character.Head.Nametag.BasicName.Visible = true
				FoundPlayer.Character.Head.Nametag.Advanced.Visible = false
			end
		end
	end
end)

Right now, it isn’t working, and I can’t see why.
Any help would be much appreciated, as it is going towards a HUGE project of mine!!! :smiley:

1 Like

Have you thought about using mouse move instead of renderstepped or is there a reason?
Also you should use some prints in your script to determine what if statements it is passing correctly – this can help greatly with debugging

did you have any errors?

Also your possibly setting foundplayer but never unsetting it to say nil and your if FoundPlayer then is actually not going to be fired to remove the highlight etc in its current position correctly so maybe try something like this

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local FoundPlayer  -- this was missing in your code so added

RunService.RenderStepped:Connect(function()
	local Target = Mouse.Target

	if Target and Target.Parent:FindFirstChild("Humanoid") then
		print('Found Humanoid')
		local FoundPlayer = Players:GetPlayerFromCharacter(Target.Parent)
		if FoundPlayer then
			print('Set FoundPlayer')
			local FCharacter = FoundPlayer.Character
			FCharacter.DetailHighlight.Enabled = true
			FCharacter.Head.Nametag.BasicName.Visible = false
			FCharacter.Head.Nametag.Advanced.Visible = true
			FCharacter.Head.Nametag.Advanced.Exp.lit.req.Text = FoundPlayer.lvl.Value
		end
	elseif FoundPlayer then
		print('Unset FoundPlayer')
		local FCharacter = FoundPlayer.Character
		FCharacter.DetailHighlight.Enabled = false
		FCharacter.Head.Nametag.BasicName.Visible = true
		FCharacter.Head.Nametag.Advanced.Visible = false
		FoundPlayer = nil
	end
end)

also added some print in there so you can see what i mean… You will need to remove these before going live else it could cause client lagg

1 Like

This still doesnt deactivate after the mouse has moved. Are there any ways this can be fixed??

does it not print unset foundplayer?

1 Like

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