For future reference, there is a Blue Icon which has the word Lua is what we call a CodeBlock. Place your code inside the CodeBlock.
I can see that you have posted an incomplete code. I will try to, however, help you as best as I can.
So first of all, it is important to organize your code properly because it is generally much easier to debug. Debug is process of identifying and eliminating any errors found in a particular scope of code. In this case, it being your Transparency
.
Now, I particularly do not like the Script.Parent
method. It does not tell us what Parent
is. It could be an IntValue
, a Remote Event
; you get the point. It is best to use what I call Hierarchy Precedence
which essentially is going from top to bottom.
Let's re write the code now to make it readable.
03 | local Workspace = game:GetService( "Workspace" ); |
04 | local UserInputService = game:GetService( "UserInputService" ); |
07 | local Part = Workspace:WaitForChild( "Part" ); |
10 | local ChangeTransparency = function (Input, GameProcessedEvent) |
11 | if Input.KeyCode = = Enum.KeyCode.E then |
12 | print ( "E was pressed!" ); |
14 | print ( "Don't care about rest" ); |
19 | UserInputService.InputBegan:Connect(ChangeTransparency); |
This code works. But here is the part i think you messed up. I do not know if you want to make a Part's transparency or Model's. See the difference is that, Model consists of group of parts. I cannot just do Model.Transparency
. That does not work. That would throw me an error which is what I think is your error.
I will show you both ways; i.e for parts and for models.
Parts
Parts is relatively easy. We just change the Transparency
property of that particular part.
03 | local Workspace = game:GetService( "Workspace" ); |
04 | local UserInputService = game:GetService( "UserInputService" ); |
07 | local Part = Workspace:WaitForChild( "Part" ); |
10 | local ChangeTransparency = function (Input, GameProcessedEvent) |
11 | if Input.KeyCode = = Enum.KeyCode.E then |
12 | print ( "E was pressed!" ); |
13 | Part.Transparency = 0.5 ; |
14 | Part.CanCollide = false ; |
16 | print ( "Don't care about rest" ); |
21 | UserInputService.InputBegan:Connect(ChangeTransparency); |
Now for models, it is a bit tricky. We will have to find all the parts inside the model, loop through all the parts and change their Transparency and Cancollide
.
03 | local Workspace = game:GetService( "Workspace" ); |
04 | local UserInputService = game:GetService( "UserInputService" ); |
07 | local ModelName = Workspace:WaitForChild( "ModelName" ); |
10 | local ChangeTransparency = function (Input, GameProcessedEvent) |
11 | if Input.KeyCode = = Enum.KeyCode.E then |
12 | print ( "E was pressed!" ); |
14 | for __, parts in pairs (ModelName:GetChildren()) do |
15 | parts.CanCollide = 0.5 ; |
16 | parts.Transparency = 0.5 ; |
19 | print ( "Don't care about rest" ); |
24 | UserInputService.InputBegan:Connect(ChangeTransparency); |
Explanation
The Generic For Loop
is a type of loops that works as an iterator function. What we are doing here is that we got the list of parts inside the model and changed their properties.
Warning. If you implemented anything inside the model that is not a part then it will throw an error.
If this is your case then you would have to find the parts first then. This is the fix to that.
03 | local Workspace = game:GetService( "Workspace" ); |
04 | local UserInputService = game:GetService( "UserInputService" ); |
07 | local ModelName = Workspace:WaitForChild( "ModelName" ); |
10 | local ChangeTransparency = function (Input, GameProcessedEvent) |
11 | if Input.KeyCode = = Enum.KeyCode.E then |
12 | print ( "E was pressed!" ); |
14 | for __, parts in pairs (ModelName:GetChildren()) do |
15 | if parts.Name = = "Part" then |
16 | parts.CanCollide = 0.5 ; |
17 | parts.Transparency = 0.5 ; |
21 | print ( "Don't care about rest" ); |
26 | UserInputService.InputBegan:Connect(ChangeTransparency); |
Magnitude property is a Robox Mathematical term for Pythagorean Theorem which essentially gets the length of a vector. To find the distance between you and a part, you would use magnitude
.
I will not go on full details because there is a character limit but the syntax for magnitude
is as follows
Character.Torso.Position - part.Position).magnitude
You can look up Magnitude
If you are confused with the wiki, take a look at this Video
[EDIT] For some of you who did not understand why I did Workspace = game:GetService("Workspace");
, it is highly recommended to do so. Because the workspace could be renamed (Although I don't see the reason why it would be), I tend to write it that way. It performs the same function as a normal Workspace
.
Also, writing either way is a person's preference. Both of these functions are the same. local function Var()
and local Var = function()
. It is similar to the method
's syntax which is: Part:Destroy()
is the same as Part.Destroy(Part)
. Either way works and it is mainly user's preference......