I don't know what I'm doing wrong (scripting problem)

So I’m trying to code a script to check what the player is clicking. The problem is that whenever I click something it chooses a random object from workspace instead of what I am actually clicking. (sometimes its the right object by chance). I’m a little confused as to what I’m doing wrong.

I’m pretty new to this so be gentle, I’ve been teaching myself.

After clicking one of the red nodes that are within the Nodes group, I got this output. Btw I’m clicking a few times just in case.

Tried again, Tile2 is within Map2.

Here’s the code, it’s a localscript being injected into the player when they join.
I’ve been doing some testing with it so obviously ignore the commented out areas and the function below.

Thx! :grin:

1 Like

The issue is that you are referencing the part that the mouse is over when the script runs

To fix this just put Mouse.Target into the connection functions

Mouse.Button1Down:connect(function()
local Target = Mouse.Target
end)
2 Likes

Oh, thanks a lot! :+1:

It’s one of the learning curves to scripting…
When referencing a variable with another variable it docent call the value of the variable it just assigns the new variable the value of the original one

Example:

local Original = 2
local New = Original
Original = 4
print(New)

This would print out 2 because you are just assigning value, there is no reason that it would update to the new value of 4

2 Likes