Input not being detected

Hi! I’m currently making a lamp system and want to detect when the player presses T to toggle the lamp, but from my current code it isn’t even detecting the press. Does anyone know how I can fix this?

This script is a localscript in StarterGui.

local Trigger = game.ServerStorage.Triggers.Light
local userInputService = game:GetService("UserInputService")
local Enabled = false
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local humanoidRootPart = Player.Character:FindFirstChild("HumanoidRootPart")

Player.CharacterAdded:Wait()

function letThereBeLight(humanoidRootPart)
	local light = Instance.new("PointLight")
	light.Parent = humanoidRootPart
	light.Brightness = 1
	light.Color = Color3.new(0.843137, 1, 0.796078)
	light.Enabled = true
	light.Range = 8
	light.Shadows = true
end

letThereBeLight(humanoidRootPart)

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	
	if input.KeyCode == Enum.KeyCode.T then
		print("you q pressed")
	end
	
end)

Replace this line.

game:GetService("UserInputService").InputBegan:Connect(function(input, IsMessaging)
	if IsMessaging then return end
	if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.T) then
		print("T was pressed!")
	end
end)

Hi, for some reason it still isn’t detecting me pressing T for some reason?
(I’m spamming T in video below)

Just delete that line. CharacterAdded signal has already been sent before the script is loaded so it’s hanging there.

Try removing the if IsMessaging.

For some reason it appears the script isn’t running at all as I added some prints to see where it was messing up and it isn’t running any of them.

local Trigger = game.ServerStorage.Triggers.Light
local Enabled = false
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local humanoidRootPart = Player.Character:FindFirstChild("HumanoidRootPart")

print("hello")
function letThereBeLight()
	print("Function work")
	local light = Instance.new("PointLight")
	light.Parent = humanoidRootPart
	light.Brightness = 1
	light.Color = Color3.new(0.843137, 1, 0.796078)
	light.Enabled = true
	light.Range = 8
	light.Shadows = true
end
print("hello world")
letThereBeLight()
print("Hi")

game:GetService("UserInputService").InputBegan:Connect(function(input, IsMessaging)
	if IsMessaging then 
		return 
	end
	if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.T) then
		print("T was pressed!")
	end
end)

The script isn’t running at all so I don’t know what’s happening.

It’s stopping because it can’t find Triggers on line 1.
Delete that line and the script runs fine on my system.

2 Likes
local Enabled = false
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local humanoidRootPart = Player.Character:FindFirstChild("HumanoidRootPart")

print("hello")
function letThereBeLight()
	print("Function work")
	local light = Instance.new("PointLight")
	light.Parent = humanoidRootPart
	light.Brightness = 1
	light.Color = Color3.new(0.843137, 1, 0.796078)
	light.Enabled = true
	light.Range = 8
	light.Shadows = true
end
print("hello world")
letThereBeLight()
print("Hi")

game:GetService("UserInputService").InputBegan:Connect(function(input, IsMessaging)
	if IsMessaging then 
		return 
	end
	if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.T) then
		print("T was pressed!")
	end
end)

Try this

1 Like