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

How does one create a door using .KeyDown?

Asked by 7 years ago

First of all, i suck at scripting. LmAo. So dooonttt judge. Anyway tried to create a script using Cframe and with bool values. It didnt work. I neeed heelp.

01local Player = game.Players.LocalPlayer
02local Mouse = Player:GetMouse()
03local IsOpen = script.Parent.IsOpen
04local RightDoor = script.Parent.RightDoor
05script.Parent.Parent.PrimaryPart = script.Parent.Parent.Primary
06 
07if (workspace.Player1.Torso.Position - workspace.Part.Position).magnitude > 20 then
08Mouse.KeyDown:connect(function(Key)
09    if Key == "e" then
10       script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(1.5,0,0))
11       elseif Key == "e" then
12 script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(-1.5,0,0))
13    end
14end)

1 answer

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
7 years ago
Edited 7 years ago

The Event KeyDown is deprecated, so use the UserInputService instead

As for actually scripting the door, you would have to use a RemoteEvent to do that, as with Filtering Enabled on, other players won't see it. Basically, instructions are below;

Put a RemoteEvent in the ReplicatedStorage named DoorOpen

LocalScript

01local Player = game.Players.LocalPlayer
02local userinput = game:GetService("UserInputService")
03local door = workspace.Door -- define the door
04 
05userinput.InputBegan:Connect(function(input,processed)
06    if input.KeyCode == Enum.KeyCode.E and Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") and not processed then
07        if (door.Position-Player.Character.HumanoidRootPart.Position).magnitude < 20 then
08            game.ReplicatedStorage:WaitForChild("DoorOpen"):FireServer()
09        end
10    end
11end)

Script

01game.ReplicatedStorage:WaitForChild("DoorOpen")
02local door = workspace.Door -- define the door again
03 
04game.ReplicatedStorage.DoorOpen.OnServerEvent:Connect(function(player)
05    if not open then
06        -- open door
07    else
08         -- close door
09    end
10    open = not open
11end)

The reason it didn't work is that firstly, this script, by the looks of it, wouldn't work due to the fact that a LocalScript will only work if it is in the Player's PlayerGui, Backpack, Character, PlayerScripts or ReplicatedFirst. Looking at the code, I can tell that this script is probably located in the workspace, a model in which only a server script (Script) can run in.

Even if you did change this to a server script, it still wouldn't work as LocalPlayer is only accessible to the client, and even if it worked as a Local script, Filtering Enabled will stop the client from sending the door's CFrame to the Server, and in turn stop the server from replicating that to all the other clients (players), so ultimately, you will see the change, and others won't. To stop this from happening, you'd use a RemoteEvent

When scripting though, practice makes perfect, and all this will eventually make sense.

0
I'm starting to get what you are saying, but where would I put the Local Script and Script? LiLFriks 39 — 7y
0
playergui greatneil80 2647 — 7y
0
The Local Script would be in the PlayerGui, and the script would preferrably be inside the door. However, if they are both in places they can run in, that's fine. UgOsMiLy 1074 — 7y
Ad

Answer this question