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 3 years ago
Edited 3 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.

local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local tar = mouse.Hit.p
local function Trans(partLookedAt)
    tar.Transparency = tar.Transparency - 0.2
end

tool.Equipped:Connect(function(mouse)
    mouse.Button1Down:Connect(function()
        for i,tar in pairs(workspace:GetDescendants()) do
            if tar.Name == "BOBJECT" then do
                    print(tar)
                    Trans()
                end
            end
        end
    end)

end)

mouse.Button1Down:Connect(Trans)
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 — 3y
0
i found a solution, only problem is its printing and it still wont change the transparency. GlidingPlane 28 — 3y

1 answer

Log in to vote
1
Answered by 3 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.

local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()

local function Trans(object)    
    object.Transparency = object.Transparency - 0.2
end

tool.Activated:Connect(function()
    local tar = mouse.Target
    if tar.Name == "BOBJECT" then
        Trans(tar)
    end
end)

**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