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:
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)
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)
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)
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
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)
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.
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?
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)
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
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:
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)