Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What am I doing wrong with this script to open a door by pressing E?

I am trying to make the script open a door when I press E, but I can't get it to actually open. My script doesn't have errors as far as I know. I also don't know about scripting at all so I may be doing this ENTIRELY wrong. Here is my script;

local UserInputService = game:GetService("UserInputService")
local motor = game.Workspace.main.Motor
 
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local keyPressed = input.KeyCode
		if keyPressed == Enum.KeyCode.E then
			motor.DesiredAngle = 1
		end
	end
end)

Lot of it was kinda salvaged from the wiki, but it doesn't work. I just need to know what I'm doing wrong, since Roblox won't tell me.

2 answers

✓ Accepted answer

this is so wrong, do this instead

local UserInputService = game:GetService("UserInputService")
local motor = game.Workspace.main.Motor

if UserInputService:IsKeyDown(Enum.KeyCode.E) then--simply checks if you click e
motor.DesiredAngle = 1
end

its probably better if you use IsKeyDown otherwise you will have to do something on inputended

this will do what you want to when the player clicks e otherwise if you really do want the input began function ill show you below

local motor = game.Workspace.main.Motor
game:GetService("UserInputService").InputBegan:Connect(function(input,gameprocess)
if input == Enum.KeyCode.E then--just go directly for the button, there is no need to go for the keyboard
motor.DesiredAngle = 1
end
end)

I'm assuming that this is a localscript and not a server-side script. And if you didn't know localscripts and things like motors and body(thrust, position, etc.) are kinda wonky when changed through a local script.

You could simply tie up the system to a remoteEvent that closes and opens the door server-side, thus eliminating the bugs of changing them with a local script.

This discussion is preserved as it was. New answers, comments, and votes are closed.