Ok, So I have the following script inside a hatch
function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.E then script.Parent.Transparency = 0.6 end end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
It Should set the transparency, then I would have addded a part to make it Notcollide, (So you can walk through)
Here's the thing
It Errors trying to change the transparency,
Also you could press E any where in the game for it to happen
Any way to fix this, and also force people to have to be right next to it, to happen?
Hope someone can help!
Thanks :D
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.
-- [Declaration Section] -- //Game Services local Workspace = game:GetService("Workspace"); local UserInputService = game:GetService("UserInputService"); -- //Part/Model Location local Part = Workspace:WaitForChild("Part"); -- [Processing Section] 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; -- [Connecting Section] 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 is relatively easy. We just change the Transparency
property of that particular part.
-- [Declaration Section] -- //Game Services local Workspace = game:GetService("Workspace"); local UserInputService = game:GetService("UserInputService"); -- //Part/Model Location local Part = Workspace:WaitForChild("Part"); -- [Processing Section] 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; -- [Connecting Section] 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
.
-- [Declaration Section] -- //Game Services local Workspace = game:GetService("Workspace"); local UserInputService = game:GetService("UserInputService"); -- //Part/Model Location local ModelName = Workspace:WaitForChild("ModelName"); -- [Processing Section] 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; -- [Connecting Section] UserInputService.InputBegan:Connect(ChangeTransparency);
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.
If this is your case then you would have to find the parts first then. This is the fix to that.
-- [Declaration Section] -- //Game Services local Workspace = game:GetService("Workspace"); local UserInputService = game:GetService("UserInputService"); -- //Part/Model Location local ModelName = Workspace:WaitForChild("ModelName"); -- [Processing Section] 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; -- [Connecting Section] 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......
local m = game.Players.LocalPlayer:GetMouse() db = true m.KeyDown:connect(function(k) k = k:lower() if k == "e" then --Change "e" to the key you want to be pressed down if db = true then game.Workspace.(Part name).Transparency = 0.6 -- Changes transparency db = false wait (5) --Waits 5 seconds before changing transparency back game.Workspace.(Part name).Transparency = 0 db = true
Hope this helped. If you want it to be can-collide (so you can walk through) under where it changes transparency add "game.Workspace.(Part name).cancollide = true" If needed fix grammar.