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

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

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?

2 comments

0

did you click Q or left shift because the Enum is Q and the print is "left shift" (Whats up with the logic)

0

i forgot to update it :)

1 answer

✓ Accepted answer
  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.

2 comments

0

thanks it helped! im going to accept your answer but what is with the "gameProcessedEvent"?

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

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