Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

my user input service script not working?

Asked by
seikkatsu 110
4 years ago
------------------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

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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.

0
it doesn't work and i don't get any error seikkatsu 110 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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.

Answer this question