PlayerMouse
For something as simple as having a name tag appear near the mouse when you hover over an object, you shouldn't need to use RayCasting at all. In fact, this can be accomplished with the Move event and Target property of the PlayerMouse.
Explanation
The goal is to make it so whenever the mouse moves, it will check to see what object it's hovering over, then do whatever it is we want to do. Here's an example:
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Tag = Instance.new("TextLabel",script.Parent)
Tag.Text = ""
Tag.Visible = false
Tag.TextColor3 = Color3.new(1,1,1)
Tag.Font = "ArialBold"
Tag.FontSize = "Size18"
Tag.BackgroundColor3 = Color3.new(0,0,0)
Tag.BackgroundTransparency = 0.5
Tag.TextXAlignment = "Left"
Mouse.Move:connect(function()
local Target = Mouse.Target
if Target then
Tag.Text = Target.Name
Tag.Size = UDim2.new(0,Tag.TextBounds.X,0,20)
Tag.Visible = true
else
Tag.Visible = false
end
Tag.Position = UDim2.new(0,Mouse.X-Tag.AbsoluteSize.X,0,Mouse.Y-Tag.AbsoluteSize.Y)
end)
Result should look like this: https://gyazo.com/25cca50513b0137af95a3169d42445f0 (remembering this should be in a local script, inside a ScreenGui object).
Edit
As XAXA mentioned, obviously this won't work unless you actually move the mouse. Unlike if you were to walk by something with your mouse in the same place, the code will not update. We could easily fix this by using the RenderStepped update of RunService to make it so our code will constantly check it's conditions regardless of user input. Here's an example:
local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Tag = Instance.new("TextLabel",script.Parent)
Tag.Text = ""
Tag.Visible = false
Tag.TextColor3 = Color3.new(1,1,1)
Tag.Font = "ArialBold"
Tag.FontSize = "Size18"
Tag.BackgroundColor3 = Color3.new(0,0,0)
Tag.BackgroundTransparency = 0.5
Tag.TextXAlignment = "Left"
RunService.RenderStepped:connect(function()
local Target = Mouse.Target
if Target then
Tag.Text = Target.Name
Tag.Size = UDim2.new(0,Tag.TextBounds.X,0,20)
Tag.Visible = true
else
Tag.Visible = false
end
Tag.Position = UDim2.new(0,Mouse.X-Tag.AbsoluteSize.X,0,Mouse.Y-Tag.AbsoluteSize.Y)
end)
And that's how we'd fix that problem. Now regarding your question about making it only happen for certain parts, we could create a dictionary of parts with a true value to easily index and compare it to our Target property. Here would be the final result:
local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Objects = {
[workspace:WaitForChild("Part1")] = true,
[workspace:WaitForChild("Part2")] = true,
[workspace:WaitForChild("Part3")] = true
}
local Tag = Instance.new("TextLabel",script.Parent)
Tag.Text = ""
Tag.Visible = false
Tag.TextColor3 = Color3.new(1,1,1)
Tag.Font = "ArialBold"
Tag.FontSize = "Size18"
Tag.BackgroundColor3 = Color3.new(0,0,0)
Tag.BackgroundTransparency = 0.5
Tag.TextXAlignment = "Left"
RunService.RenderStepped:connect(function()
local Target = Mouse.Target
if Target and Objects[Target] then
Tag.Text = Target.Name
Tag.Size = UDim2.new(0,Tag.TextBounds.X,0,20)
Tag.Visible = true
else
Tag.Visible = false
end
Tag.Position = UDim2.new(0,Mouse.X-Tag.AbsoluteSize.X,0,Mouse.Y-Tag.AbsoluteSize.Y)
end)
Edit 2
For your third question, making it so you don't have to manually assign the objects and to just make it so all objects in a model are active, you could use the IsAncestorOf method on the model, with the Target property.
local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local ActiveParts = workspace:WaitForChild("SomeModel")
local Tag = Instance.new("TextLabel",script.Parent)
Tag.Text = ""
Tag.Visible = false
Tag.TextColor3 = Color3.new(1,1,1)
Tag.Font = "ArialBold"
Tag.FontSize = "Size18"
Tag.BackgroundColor3 = Color3.new(0,0,0)
Tag.BackgroundTransparency = 0.5
Tag.TextXAlignment = "Left"
RunService.RenderStepped:connect(function()
local Target = Mouse.Target
if Target and ActiveParts:IsAncestorOf(Target) then
Tag.Text = Target.Name
Tag.Size = UDim2.new(0,Tag.TextBounds.X,0,20)
Tag.Visible = true
else
Tag.Visible = false
end
Tag.Position = UDim2.new(0,Mouse.X-Tag.AbsoluteSize.X,0,Mouse.Y-Tag.AbsoluteSize.Y)
end)
Hope that helped. If you have any questions, just let me know.
4 comments
you don't need to raycast go to the roblox wiki and search up the mouse object
Can you link me?
http://wiki.roblox.com/index.php?title=API:Class/Mouse/Target that should help
creating Objects, CFrame,Services ,Events, PlayerGui, DataStore, RayCast that is the best order to learn how to do things in roblox.