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

How do I fix Transparency is not a valid member of Vector3 when using mouse.Hit.p?

Asked by 4 years ago
Edited 4 years ago

Hello, I'm trying to change the transparency of an item by clicking it with a tool. Kind of new to FE coding and what not so, heres what I've gotten so far, but I keep getting the error mentioned above.

"BOBJECT" is the part im testing with. I was using print to see if it got that far, and it did. I've looked everywhere online to a solution, any help would appreciated.

01local tool = script.Parent
02local mouse = game.Players.LocalPlayer:GetMouse()
03local tar = mouse.Hit.p
04local function Trans(partLookedAt)
05    tar.Transparency = tar.Transparency - 0.2
06end
07 
08tool.Equipped:Connect(function(mouse)
09    mouse.Button1Down:Connect(function()
10        for i,tar in pairs(workspace:GetDescendants()) do
11            if tar.Name == "BOBJECT" then do
12                    print(tar)
13                    Trans()
14                end
15            end
View all 21 lines...
0
To my understandings, mouse.hit.p is a Vector3 property. I think there is something wrong in your script. (i cant find a solution of this because my head hurts even looking at this (my scripts skills are not really advanced though)) rayhansatria 142 — 4y
0
i found a solution, only problem is its printing and it still wont change the transparency. GlidingPlane 28 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

Alright.

The first thing that I noticed is that mouse.Hit.p gets the position of the mouse. What you were essentially trying to do is find an object that is equal to a position, which you can't do. Secondly, you got mouse.Hit.p at the start of the script. Once the script is ran, 'tar' is defined and will never change.

Thankfully for you, the mouse has a property called 'Target', which is the part the mouse is currently on top of.

I rewrote some stuff, and here's what I came out with.

01local tool = script.Parent
02local mouse = game.Players.LocalPlayer:GetMouse()
03 
04local function Trans(object)   
05    object.Transparency = object.Transparency - 0.2
06end
07 
08tool.Activated:Connect(function()
09    local tar = mouse.Target
10    if tar.Name == "BOBJECT" then
11        Trans(tar)
12    end
13end)

**By the way, the reason you were getting an error was because whenever the mousebutton was pressed, it fired the Trans function (line 21). Since tar was already defined, and was probably nil, the function had to change the transparency of a nil value, which it can't do.

(tool.Activated is just a more convenient mouse.Button1Down for tools that fires whenever the mouse button is pressed and the tool is equipped)

Ad

Answer this question