Mouse Following a GUI using Mouse.Target

Hello, Fellow Devs! My Discord is on lock rn so I can’t really ask any discord servers until further notice from discord. Anyways, I was working on a system to check The terrain on which the player chose for the landing system of my game. Here’s the “Martian Map”.


As you can see the map has some black/brownish spots (Mountainous), The one without (Plains), And lastly the Artic.

I want to check which did the player select. And I had an idea of using Mouse.Target To return the name and the Selection of the player. I had a hitbox already (those whitish outlines of the map) to check what terrain did the player choose. Ik this might be inefficient but I decided it would be a test. So I smacked the code and so far the only problems I encounter are the following.

The frame doesn’t follow the mouse smoothly

The same error on this Tween code.

local TweenMouse = TweenServ:Create(Frame,TweenInfo.new(.01,Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Position = UDim2.new(0, Mouse.X, 0, Mouse.Y)})

Error:
Attempt to call a TweenInfo Value

Code to check the terrain, error had the problem with .Name

if Mouse.Target.Name == "Plains" or "Mountains" or "Artic" then

11:12:44.309 - Players.cool900s.PlayerGui.LocalScript:19: attempt to index nil with 'Name'
And yes I’ve tried asking the RoDevs Discord (Before) and searching up here on the DevForums, I haven’t really looked this problem when I encountered it because I had Online Classes.

Take note I don’t want to be spoonfed that much. Im a moderate Scripter where I know some complicated stuff like RayCast, CFrame, and etc. The Problem is I haven’t played around it so I would have no idea what to do if I encounter a problem. Please take it light on me :sweat_smile: , Ive been modelling for the past few weeks and just got back to scripting. Thanks in Advance!

1 Like

Problem here is that youre asking for the Mouse.Target name immediately.
Mouse.Target will become nil if your mouse isn’t hovering over anything. So a fix would be:

if Mouse.Target and Mouse.Target.Name == "Plains" or "Mountains" or "Artic" then

As for your other problems, I don’t really have enough information to know what’s up. Hope this helped!

2 Likes

Thanks! I’ll Check it out soon.

1 Like

Woah! It seems you’re right i changed some few then came up with this. Thanks, all i need to finish now is the smoothness of frame/ui to the mouse’s movement

Just came here to say that I highly recommend that you get around to utilizing the UserInputService, because the staff have stated that accessing the Mouse this way will be deprecated soon (to be unsupported).

Right now it’s just a legacy feature, but I wouldn’t doubt that they would phase this out.

Oh, thats pretty sad… Anyways, any links to this?

(Apologies on mobile)

Tutorial on Wiki: UserInputService | Documentation - Roblox Creator Hub

Lost the link to where they replied about this, but I’m sure you can search around.

You’re welcome to still use Mouse for now. But they are heavily discouraging this, hence why in the disclaimer they’re encouraging new developers to use this method.

Ah I see. ill try to use this. Thanks.

Edit: Ive checked it out and couldn’t find any other thing related to something like Mouse.Target, The closest thing ive found was :GetMouseLocation(), which looks up a Vector.2 value. Correct me if im wrong.

That code wouldn’t work because it’d always return true as all strings aren’t nil, you aren’t checking if the Mouse.Target’s name is said string, you’re checking if it exists, which it always will, so this is how you can fix it.

if Mouse.Target then
    local Name = Mouse.Target.Name;
    if Name == 'Plains' or Name == 'Mountains' or Name == Arctic then
        -- do stuff
    end
end

EDIT: You’re checking if the string exists, not the name, my bad.

Yeah, I think i’ve done that. Here’s a snippet.

--I broke it into few if statements.

while SelectMap == true then

if Mouse.Target then

if Mouse.Target == "Mountain" then --etc
--CODE
end
end
end

you get the idea.

I think this (your reply) makes things much simpler.

it should be

--I broke it into few if statements.

while SelectMap == true do

if Mouse.Target then

if Mouse.Target == "Mountain" then --etc
--CODE
end
end
end

Mouse.Target is just an object value of what the Player’s mouse is currently hovering over, or nil. You can’t compare it to a string such as ‘Mountain’ because once again, it isn’t a string, you should be using Mouse.Target.Name after checking if Mouse.Target exists.

image
here’s an actual snippet.

Why are you checking Mouse.Move? That’s an event.

Yeah, good point. But this would be only used once, but if it has something to do preformance wise then I need to recode it.

Take note: IM A MODERATE SCRIPTER REEEEEEe

I’d just remove it entirely. There’s really no point of it being there.

1 Like

Alright ill try to do so. I think it does add performance wise. (Why did I even add it there, eh scripters makes mistakes.)

There’s still thing’s i didnt get here. Such as the smooth movement of the GUI to the mouse position and if I should use UIS like what @Clueless_Brick said , but as I said:

Correct me if I’m wrong.

I don’t see how UIS would help, have you tried Tweening the GUi?

As he said.

Ive tried but it keeps saying its an TweenInfo Value no idea. Here’s a qoute.