See other players health

basically, whenever you hover your mouse over a player within a certain distance of them being near you, a UI will show up with their name and health, im guessing this is somewhat simple and im missing something, here is an example:

(ignore me not cropping the image lol)

1 Like

You probably are

Well, you’d need to detect when the Mouse moves using the Move Event & frequently checking for a Mouse.Target property

This property gives the BasePart if the mouse is hovering over a specific part, if there’s no hovered part then it’d equal nil

Adding this onto with using the GetPlayerFromCharacter() function, you can check if the Mouse’s Target is hovering over 1 of the Character’s Parts or not to show a UI

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local UI = --UI to show here

Mouse.Move:Connect(function()
    local Target = Mouse.Target

    local CharTarget = game.Players:GetPlayerFromCharacter(Target.Parent)

    if CharTarget then
        UI.Visible = true
    else
        UI.Visible = false
    end
end)
3 Likes

Torso doesnt exist, apparently,

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local UI = Player.PlayerGui:WaitForChild("PlayerHealth") --UI to show here

	Mouse.Move:Connect(function()
	local Target = Mouse.Target

	local CharTarget = game.Players:GetPlayerFromCharacter(Target.Parent)
	if Target == CharTarget:WaitForChild("Torso") then
		UI.Visible = true
	else
		UI.Visible = false
	end
	--[[
	if CharTarget then
		UI.Visible = true
	else
		UI.Visible = false
	end
	--]]
end)
1 Like

You need to implement a sanity check to ensure that there is an actual CharTarget property, otherwise it’d default as some error or a nil value

Use the code I referenced earlier

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local UI = Player.PlayerGui:WaitForChild("PlayerHealth")

Mouse.Move:Connect(function()
    local Target = Mouse.Target

    local CharTarget = game.Players:GetPlayerFromCharacter(Target.Parent)

    if CharTarget and Target.Name == "Torso" then
        UI.Enabled = true
    else
        UI.Enabled = false
    end
end)
3 Likes

how about the players body parts instead of the torso? and how would the UI display the other players health, and

i have that covered, its more of the displaying the health

1 Like

CharTarget would already cover the sanity check? For the Target’s Health, you could create a TextLabel inside your GUI then reference said “TextLabel” to change the text for that specific target

TextLabel.Text = CharTarget.Name

While you are asking this question, I scripted this. This is done by using heartbeat to update the GUI, get :DistanceFromCharacter() for showing over a certain distance, and mouse to detect visibility. Yes, this also detects accessories and body parts.

Full code
local rs = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
local ui = script.Parent.Parent.Parent
local frame = script.Parent.Parent

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local HPframe = script.Parent 
local text = script.Parent.Parent.NamePLR
local maxDist = 10

rs.Heartbeat:Connect(function()
	local target = mouse.Target 
	if target then
		local CharTarget = game.Players:GetPlayerFromCharacter(target.Parent) -- bodypart
			or game.Players:GetPlayerFromCharacter(target.Parent.Parent) -- accessory
		
		if CharTarget then
			local head = CharTarget.Character:FindFirstChild("Head")
			if head then
				local dist = plr:DistanceFromCharacter(head.Position)
				if dist < maxDist then
					frame.Visible = true
					text.Text = CharTarget.Name
					local charTarget = CharTarget.Character
					HPframe.Bar.Size = UDim2.new(0,(charTarget.Humanoid.Health/charTarget.Humanoid.Health * 200),1,0)
				else
					frame.Visible = false
				end
			end
		else
			frame.Visible = false
		end
	end
end)

Results:

Rbxm file: gui2Fixed.rbxm (5.2 KB)

Edit: I’m assuming there is some GUI health bar offset issue so I’m going to try to fix that.
Edit2: Fixed it, but I didn’t test it but should work.

isnt that the characters name? would i do TextLabel.Text = CharTarget.Humanoid.Health?
or, a bar with a Udim2

1 Like

That’s if you want to show the name (Provided in your OP that you showed)

If you wanted to just show the health, you can do that too using CharTarget.Humanoid.Health

1 Like

well, i cant, as ive already tried that, and there is an error, multiple, but here is the main one
Attempt to index nil with Humanoid

1 Like

Wait I’m stupid

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local UI = Player.PlayerGui:WaitForChild("PlayerHealth")

Mouse.Move:Connect(function()
    local Target = Mouse.Target

    local PlayerTarget = game.Players:GetPlayerFromCharacter(Target.Parent)

    if PlayerTarget then
        local CharTarget = Target.Parent

        --Now do your HP change here
        UI.Enabled = true
    else
        UI.Enabled = false
    end
end)

Also why would it say that it’d be nil if there was already a change detected?

1 Like

Give nil because, when pointing to the sky, there is nothing to return.
Only this line is missing below Target:

if not Target then			return			end
2 Likes

attempt to index nil with parent

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local UI = Player.PlayerGui:WaitForChild("PlayerHealth")
local TweenService = game:GetService("TweenService")
local TextLabel = UI:WaitForChild("Frame").TextLabel
--[[
local UITweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local UI = TweenService:Create(UI, UITweenInfo, {Transparency = 1})

local UITweenInfo2 = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween = TweenService:Create(UI, UITweenInfo2, {Transparency = 0})
--]]

Mouse.Move:Connect(function()
	local Target = Mouse.Target

	local PlayerTarget = game.Players:GetPlayerFromCharacter(Target.Parent)

	if PlayerTarget then
		if not Target then
			return
		end
		local CharTarget = Target.Parent

		TextLabel.Text = CharTarget.Humanoid.Health--Now do your HP change here
		UI.Enabled = true
	else
		UI.Enabled = false
	end
end)

Reference the if not Target then statement when you first define it

Mouse.Move:Connect(function()
	local Target = Mouse.Target

	if not Target then
		return
	end

	local PlayerTarget = game.Players:GetPlayerFromCharacter(Target.Parent)

	if PlayerTarget then

		local CharTarget = Target.Parent

		TextLabel.Text = CharTarget.Humanoid.Health--Now do your HP change here
		UI.Enabled = true
	else
		UI.Enabled = false
	end
end)
1 Like

no errors, but also nothing happens

Perhaps try printing, and see what works & what doesn’t?

1 Like
Mouse.Move:Connect(function()
	local Target = Mouse.Target
	if not Target then
		return
	end

	local PlayerTarget = game.Players:GetPlayerFromCharacter(Target.Parent)

	if PlayerTarget then

		local CharTarget = Target.Parent
		print("Found Target")
		TextLabel.Text = CharTarget.Humanoid.Health--Now do your HP change here
		print("TextLabel Text = Humanoids health")
		UI.Enabled = true
		print("Enabled")
	else
		UI.Enabled = false
		print("Disabled")
	end
end)

prints everything,

and still only works when the mouse is over the torso

1 Like

When you mean “nothing happens”, do you mean the UI isn’t showing up?

yeah basically, everything is still printing though

this is what im using now:

Mouse.Move:Connect(function()
	local Target = Mouse.Target
	if not Target then
		return
	end

	local PlayerTarget = game.Players:GetPlayerFromCharacter(Target.Parent)
	
	local head = PlayerTarget.Character:FindFirstChild("Head")
	local dist = Player:DistanceFromCharacter(head.Position)
	if dist < maxDist then
		UI.Visible = true
		TextLabel.Text = PlayerTarget.Name
		local charTarget = Player.Character
		UI.Frame.Frame.Size = UDim2.new(0,(PlayerTarget.Humanoid.Health/PlayerTarget.Humanoid.Health * 200),1,0)
	else
		UI.Visible = false
	end
end)

tried this and it didnt work, no errors, this is my UI layout:

Screen Shot 2021-05-26 at 5.51.08 PM

local script in startercharacter:

local rs = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()



local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local ui = plr.PlayerGui:WaitForChild("PlayerHealth")
local frame = ui.Frame

local HPframe = plr.PlayerGui:WaitForChild("PlayerHealth").Frame.Frame
local text = frame.TextLabel
local maxDist = 10

rs.Heartbeat:Connect(function()
	local target = mouse.Target 
	if target then
		local CharTarget = game.Players:GetPlayerFromCharacter(target.Parent) -- bodypart
			or game.Players:GetPlayerFromCharacter(target.Parent.Parent) -- accessory

		if CharTarget then
			local head = CharTarget.Character:FindFirstChild("Head")
			if head then
				local dist = plr:DistanceFromCharacter(head.Position)
				if dist < maxDist then
					frame.Visible = true
					text.Text = CharTarget.Name
					local charTarget = CharTarget.Character
					HPframe.Size = UDim2.new(0,(charTarget.Humanoid.Health/charTarget.Humanoid.Health * 200),1,0)
				else
					frame.Visible = false
				end
			end
		else
			frame.Visible = false
		end
	end
end)