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

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local IsOpen = script.Parent.IsOpen
local RightDoor = script.Parent.RightDoor
script.Parent.Parent.PrimaryPart = script.Parent.Parent.Primary

if (workspace.Player1.Torso.Position - workspace.Part.Position).magnitude > 20 then
Mouse.KeyDown:connect(function(Key)
    if Key == "e" then
       script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(1.5,0,0))
       elseif Key == "e" then
 script.Parent.Parent:SetPrimaryPartCFrame(script.Parent.Parent:GetPrimaryPartCFrame()*CFrame.fromEulerAnglesXYZ(-1.5,0,0))
    end
end)

1 answer

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago
Edited 6 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

local Player = game.Players.LocalPlayer
local userinput = game:GetService("UserInputService")
local door = workspace.Door -- define the door

userinput.InputBegan:Connect(function(input,processed)
    if input.KeyCode == Enum.KeyCode.E and Player.Character and Player.Character:FindFirstChild("HumanoidRootPart") and not processed then
        if (door.Position-Player.Character.HumanoidRootPart.Position).magnitude < 20 then
            game.ReplicatedStorage:WaitForChild("DoorOpen"):FireServer()
        end
    end
end)

Script

game.ReplicatedStorage:WaitForChild("DoorOpen")
local door = workspace.Door -- define the door again

game.ReplicatedStorage.DoorOpen.OnServerEvent:Connect(function(player)
    if not open then
        -- open door
    else
         -- close door
    end
    open = not open
end)

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 — 6y
0
playergui greatneil80 2647 — 6y
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 — 6y
Ad

Answer this question