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

How could i activate a script when u press a key?

I cant find it anywhere, so i thought someone would help me :)

My script is

local cframe = model.PrimaryPart.CFrame -- Current CFrame

model:SetPrimaryPartCFrame(cframe * CFrame.Angles(0, math.rad(40), 0)) script.Disabled = true

while wait() do
	script.Disabled = true
	print(script.Disabled)
 

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
function PressedE(key)

		Mouse.KeyDown:connect(PressedE)
		
while wait() do
	script.Disabled = true
			print(script.Disabled)
			
	      end
	   end
	end

2 comments

0

Scripts has a property called disabled. You can use User input service to trigger this propperty in a script.

0

https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction An alternative method is to use ContextActionService. The link above has an example that can easily be adapted for your use-case.

1 answer

To activate a script when as i understand 'E' is pressed, you could use UserInputService

Here is script example:

Desired key is key that you want to be listened to. InputBegan is function of UserInputService and it fires whenever and input is pressed (Screen touch or gamepad button clicked or keyboard...). After it fires, there are 2 values it returns, input and mouseOnGui. Input is input that user pressed and mouseOnGui is whether the player is hovering his mouse over gui (For example when you have building game and you don't want player to place blocks when he clicks paint button, you check if mouseOnGui is false).

local SCRIPT_YOU_WANT_TO_DISABLE = script
local DESIRED_KEY = Enum.KeyCode.E

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, mouseOnGui)
	local pressedKey = input.KeyCode
	
	if pressedKey == DESIRED_KEY then
		SCRIPT_YOU_WANT_TO_DISABLE.Disabled = true or false
	end
end)

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