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