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

why doesn't my user input service script doesn't work?

Asked by
seikkatsu 110
4 years ago
local uis = game:GetService("UserInputService")
local h = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
uis.InputBegan:Connect(function(input)
    local keyIn = input.KeyCode
    if keyIn == Enum.KeyCode.Q then
        print("left shift")
                h.WalkSpeed  = 25
    end
end)

The script is parented in Starter player scripts and it's a local script What am i doing wrong?

0
did you click Q or left shift because the Enum is Q and the print is "left shift" (Whats up with the logic) Luka_Gaming07 534 — 4y
0
i forgot to update it :) seikkatsu 110 — 4y

1 answer

Log in to vote
1
Answered by
Syclya 224 Moderation Voter
4 years ago
  1. You'd want to use GetService() as it is recommended.
  2. You did not check if the character existed before continuing.

Here is the fix:

local UIS = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- character might not have been created yet.
local Humanoid = Character:WaitForChild("Humanoid")

UIS.InputBegan:Connect(function(Key, gameProcessedEvent)
    if Key.KeyCode == Enum.KeyCode.Q and not gameProcessedEvent then
        Humanoid.WalkSpeed = 25
    end
end)

Hope this helped.

0
thanks it helped! im going to accept your answer but what is with the "gameProcessedEvent"? seikkatsu 110 — 4y
0
The "gameProcessedEvent" Indicates whether the game engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent would be true. This is also true for input events connected via ContextActionService. Source: https://developer.roblox.com/en-us/api-reference/event/UserInputService/InputBegan Syclya 224 — 4y
Ad

Answer this question