I was on my sister's account when asking that last question, but that's besides the point. I was just experimenting and learning a few things. I want to try something else and I have no idea where to start;
When I move my mouse cursor to hover over a certain part, I want a gui to appear beside of the cursor with text. Then when I am no longer hovering over it, the gui will disappear.
i.e. I move my mouse over a vase and then a gray gui box with white text appears beside of my cursor saying "Vase". Then when I move my cursor to not touch the vase, the gui will disappear.
I don't really have any script, because as I said I don't know where to start. Also, someone said to add these in this order: "raycasting + Gui stuffs + Mouse events " Don't know how to use raycasting.
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 -- Get the player local Mouse = Player:GetMouse() -- Get the player's mouse -- Just creating a name tag for this example. This is assuming the script is inside a ScreenGui that's in the PlayerGui. 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" -- Connect the "Move" event to the mouse Mouse.Move:connect(function() local Target = Mouse.Target -- Get the object the mouse is hovering over if Target then -- Make sure the target exists -- Update the information when a target exists Tag.Text = Target.Name Tag.Size = UDim2.new(0,Tag.TextBounds.X,0,20) -- TextBounds.X will make the tag be as long as the text, giving it a more complimenting appearance Tag.Visible = true -- If no target exists, then... else -- Hide the tag Tag.Visible = false end -- Update the tag's position with the mouse 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 -- Get the player local Mouse = Player:GetMouse() -- Get the player's mouse -- Just creating a name tag for this example. This is assuming the script is inside a ScreenGui that's in the PlayerGui. 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" -- Using the RenderStepped event of RunService, which updates at roughly 1/60th of a second (about twice as fast as the generic wait function, obviously this should still be in a local script) RunService.RenderStepped:connect(function() local Target = Mouse.Target -- Get the object the mouse is hovering over if Target then -- Make sure the target exists -- Update the information when a target exists Tag.Text = Target.Name Tag.Size = UDim2.new(0,Tag.TextBounds.X,0,20) -- TextBounds.X will make the tag be as long as the text, giving it a more complimenting appearance Tag.Visible = true -- If no target exists, then... else -- Hide the tag Tag.Visible = false end -- Update the tag's position with the mouse 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 -- Get the player local Mouse = Player:GetMouse() -- Get the player's mouse -- Dictionary of parts that have a true (or existing) value local Objects = { [workspace:WaitForChild("Part1")] = true, [workspace:WaitForChild("Part2")] = true, [workspace:WaitForChild("Part3")] = true } -- Just creating a name tag for this example. This is assuming the script is inside a ScreenGui that's in the PlayerGui. 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" -- Using the RenderStepped event of RunService, which updates at roughly 1/60th of a second (about twice as fast as the generic wait function, obviously this should still be in a local script) RunService.RenderStepped:connect(function() local Target = Mouse.Target -- Get the object the mouse is hovering over if Target and Objects[Target] then -- Make sure the target exists, and that it's inside the exclusive Objects table -- Update the information when a target exists Tag.Text = Target.Name Tag.Size = UDim2.new(0,Tag.TextBounds.X,0,20) -- TextBounds.X will make the tag be as long as the text, giving it a more complimenting appearance Tag.Visible = true -- If no target exists, then... else -- Hide the tag Tag.Visible = false end -- Update the tag's position with the mouse 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 -- Get the player local Mouse = Player:GetMouse() -- Get the player's mouse -- Get the model we'll need to hold all our active objects local ActiveParts = workspace:WaitForChild("SomeModel") -- Just creating a name tag for this example. This is assuming the script is inside a ScreenGui that's in the PlayerGui. 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" -- Using the RenderStepped event of RunService, which updates at roughly 1/60th of a second (about twice as fast as the generic wait function, obviously this should still be in a local script) RunService.RenderStepped:connect(function() local Target = Mouse.Target -- Get the object the mouse is hovering over if Target and ActiveParts:IsAncestorOf(Target) then -- Check if the part is anywhere inside the model -- Update the information when a target exists Tag.Text = Target.Name Tag.Size = UDim2.new(0,Tag.TextBounds.X,0,20) -- TextBounds.X will make the tag be as long as the text, giving it a more complimenting appearance Tag.Visible = true -- If no target exists, then... else -- Hide the tag Tag.Visible = false end -- Update the tag's position with the mouse 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.