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

my user input service script not working?

------------------variables--------------------------------
local f = workspace.Floor
local p = game.Players.LocalPlayer
local c = p:FindFirstChild("Character")
local h = c:FindFirstChild("Humanoid")
local gui = p.PlayerGui.PressF.Frame
------------------user input------------------------------
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input)
	local keyIn = input.KeyCode
	if keyIn == Enum.KeyCode.A then
		print("A was pressed!")
	end
end)

this is the script.it's parented in a part and it's a local script why doesn't it work? any answer that explains this will be appriciated

2 answers

✓ Accepted answer

Improvements

Use WaitForChild() to make sure a part exists before using it

Use GetService() for the Players Service

InputBegan takes two parameters, the input, and a gameproccessed event (used to check if the input occurred in / on a gui)

Issues

My assumption is that the "part" you are referring to is in the workspace, however, LocalScripts will NOT run in the workspace, only Server Scripts, so I'd move your LocalScript to StarterGui instead

Revised Local Script in the StarterGui

local uis = game:GetService("UserInputService")

local p = game:GetService("Players").LocalPlayer
local c = p.Character or p.CharacterAdded:Wait()
local h = c:WaitForChild("Humanoid")

local gui = script.Parent:WaitForChild("PressF"):WaitForChild("Frame")

local f = workspace:WaitForChild("Floor")

uis.InputBegan:Connect(function(input, event)
	if input.KeyCode == Enum.KeyCode.A then
		print("A was pressed!")
	end
end)

If anything else in your code requires "script.Parent" i'd suggest making the part it refers to a local variable in your code and replacing those instances.

1 comment

0

it doesn't work and i don't get any error

LocalScripts are run by a player's client. LocalScripts only begin to run when they are a descendant of the Player or the Player's Character.

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