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.
local Workspace = game:GetService("Workspace");
local UserInputService = game:GetService("UserInputService");
local Part = Workspace:WaitForChild("Part");
local ChangeTransparency = function(Input, GameProcessedEvent)
if Input.KeyCode == Enum.KeyCode.E then
print("E was pressed!");
else
print("Don't care about rest");
end;
end;
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.
local Workspace = game:GetService("Workspace");
local UserInputService = game:GetService("UserInputService");
local Part = Workspace:WaitForChild("Part");
local ChangeTransparency = function(Input, GameProcessedEvent)
if Input.KeyCode == Enum.KeyCode.E then
print("E was pressed!");
Part.Transparency = 0.5;
Part.CanCollide = false;
else
print("Don't care about rest");
end;
end;
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.
local Workspace = game:GetService("Workspace");
local UserInputService = game:GetService("UserInputService");
local ModelName = Workspace:WaitForChild("ModelName");
local ChangeTransparency = function(Input, GameProcessedEvent)
if Input.KeyCode == Enum.KeyCode.E then
print("E was pressed!");
for __, parts in pairs(ModelName:GetChildren()) do
parts.CanCollide = 0.5;
parts.Transparency = 0.5;
end;
else
print("Don't care about rest");
end;
end;
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.
local Workspace = game:GetService("Workspace");
local UserInputService = game:GetService("UserInputService");
local ModelName = Workspace:WaitForChild("ModelName");
local ChangeTransparency = function(Input, GameProcessedEvent)
if Input.KeyCode == Enum.KeyCode.E then
print("E was pressed!");
for __, parts in pairs(ModelName:GetChildren()) do
if parts.Name == "Part" then
parts.CanCollide = 0.5;
parts.Transparency = 0.5;
end;
end;
else
print("Don't care about rest");
end;
end;
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......
1 comment
Can you put it in a code block, please?