Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a gui script when hovering over an object with a mouse?

Asked by 8 years ago

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.

0
you don't need to raycast go to the roblox wiki and search up the mouse object ProfessorSev 220 — 8y
0
Can you link me? eialexander 16 — 8y
0
creating Objects, CFrame,Services ,Events, PlayerGui, DataStore, RayCast that is the best order to learn how to do things in roblox. scottmike0 40 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

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.

0
The player can walk around and the Move event will not trigger. XAXA 1569 — 8y
0
Alright, it works great, but how do I make the script work for only on particular object? Right now, It says "part" beside the mouse when I hover over a part named "part," but when I hover over the baseplate it says "baseplate" I want the gui to disappear when it's not hovering over the specific part. Either that, or the gui doesn't appear when hovering over certain objects, such as the baseplate. eialexander 16 — 8y
0
And also as XAXA said, if I hover over an object named "part1" and then move my character without moving my mouse the gui will still say "part1" no matter what object the mouse is hovering over. eialexander 16 — 8y
0
Updated the answer. ScriptGuider 5640 — 8y
View all comments (12 more)
0
Thanks, now one last thing; is there a way to make it work for a group of parts so that I don't have to go and name everything in the group the same thing? eialexander 16 — 8y
0
Like making it so a tag will appear to all parts inside a model of something? ScriptGuider 5640 — 8y
0
Yes. Otherwise, I have to name every object in the group the same thing. eialexander 16 — 8y
0
Updated the answer. ScriptGuider 5640 — 8y
0
The final script with IsAncestorOf doesn't work. In the 23rd line Model says "W001: Unknown global 'model'" eialexander 16 — 8y
0
"Model" is supposed to be the variable reference to the model. Just replace it with "ActiveParts" since that was the reference in my example. ScriptGuider 5640 — 8y
0
Just tried again. There are no errors in the script, and nothing in the output, but it just doesn't work. eialexander 16 — 8y
0
Can you please answer ASAP. I'm a little anxious to start work on a new project. eialexander 16 — 8y
0
Seems to work fine for me, are you sure your environmental conditions are correct? ScriptGuider 5640 — 8y
0
What do you mean by "environmental conditions"? eialexander 16 — 8y
0
I tried using the second code since the 3rd and 4th are broken and it works great. However, when I tried to put in a public game, it doesn't. What can I put in the script to fix this? eialexander 16 — 8y
0
the 3rd and 4th aren't broken. you need to make sure you're placing the script in the right place, and that you've changed the code accordingly to fit your game. ScriptGuider 5640 — 8y
Ad

Answer this question