Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

(?) Press E to open a Hatch [CLOSED]

Asked by 6 years ago
Edited 6 years ago

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

0
Can you put it in a code block, please? MakeYourEscape 334 — 6y

2 answers

Log in to vote
0
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
6 years ago
Edited 6 years ago

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.

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

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

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.

-- [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......

1
Apparently the person who downvoted me, downvoted because I did local Workspace = game:GetService("Workspace"); What is wrong with it lmao.   Zafirua 1348 — 6y
0
@Zafirua I think it was because they usually do game.workspace or even just workspace Zenith_Lord 93 — 6y
Ad
Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago
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.

0
Appreciate the help, but here's a few key factors. The method you're using is deprecated. Try UserInputService instead. Secondly, make sure you're writing your code properly. Indenting is a key factor. If you're not sure about how to make it neater, refer to this article: http://wiki.roblox.com/index.php/Writing_Clean_Code And lastly, you're not really supplying your method nor are you explaining Async_io 908 — 6y
0
or why, make sure that you're explaining your answer so people can better understand it. https://scriptinghelpers.org/help/how-post-good-questions-answers Async_io 908 — 6y

Answer this question