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

How to run functions on key press?

The only way I know how to run functions in tool is on activated, how do I run then on key press? Here's the script I already made. The function is run on activated.

--alonzo12345

player = game.Players.LocalPlayer
tool = script.Parent
handle = tool.Handle
x = script.Chakram
spinning = false


function shieldup()
		wait(.5)
		local ff = Instance.new("ForceField")
		ff.Parent = tool.Parent
		game.Debris:AddItem(ff,2.5)
		local xc = x:Clone()
		xc.Parent = game.Workspace
		game.Debris:AddItem(xc,2.5)
		xc.CFrame = handle.CFrame
		local y = Instance.new("BodyVelocity")
		y.Parent = xc
		y.maxForce = Vector3.new(math.huge, math.huge, math.huge)
		y.Velocity = xc.CFrame.lookVector *0
		xc.loop:Play()
		while true do
			wait()
			xc.Position = handle.Position 
			xc.CFrame = handle.CFrame
		end

end

tool.Activated:connect(shieldup)

1 answer

✓ Accepted answer
function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.R then
		--code here
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Copied from the wiki, hope this helped.

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