When a key is pressed it will do a certain thing

How do I make it to where if for example the e key was pressed I want to make the character tp to somewhere or die so how do I make a script to do it?

1 Like
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.E then
		-- // Put code here.
	end
end)

This should be put inside a local script since user input is on the client.
For more info: UserInputService

local UIS = game:GetService("UserInputService")
local Replicated = game:GetService("ReplicatedStorage")

UIS.InputBegan:Connect(function(key,proceeded)
      if proceeded then return end
	if key.KeyCode == Enum.KeyCode.E then
		Replicated:WaitForChild("MyRemote"):FireServer()
	end
end)

–Server

local Replicated = game:GetService("ReplicatedStorage")

Replicated.MyRemote.OnServerEvent:Connect(function(sender)
   --tping
   sender.Character:PivotTo("CFrame Here")
   --killing
   sender.Character.Humanoid.Health = 0
end)