Issue with using UserInputService

Hello Developers, I require your help! I always try to use UIS but it never works when I try to use it. Today, I was bored and decided to experiment with it - but it did not work. I decided to make a basic shift to sprint script, which does not work… At all. I have included a code block below, this is the code I have tried. I tried looking at the DevWiki API reference- still did not work.

Please let me know if you can help. :slightly_smiling_face:

local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(char)

--//

local UserInputService = game:GetService("UserInputService")

--//


UserInputService.InputBegan:Connect(function(Input)
	if Input == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 26
	
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 16
	end
end)

easy fix! after each if Input put

if Input.Keycode

but here is full fix

local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(char)

--//

local UserInputService = game:GetService("UserInputService")

--//


UserInputService.InputBegan:Connect(function(Input)
	if Input.Keycode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 26
	
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 16
	end
end)
1 Like

You’ve forgot .KeyCode after the both Inputs.

Here is the fixed code:

local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(char)

--//

local UserInputService = game:GetService("UserInputService")

--//


UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 26
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 16
	end
end)

Can you mark it as solution if it worked?

2 Likes

This still does not work? I added prints into my script too…

Video to show;

The script which I used;

local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(char)

--//

local UserInputService = game:GetService("UserInputService")

--//


UserInputService.InputBegan:Connect(function(Input)
	print("input detected")
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		print("keycode was lefshift")
		hum.WalkSpeed = 26
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	print("input detected")
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		print("keycode was lefshift")
		hum.WalkSpeed = 16
	end
end)

Oh, make sure your code is a LocalScript.
Since LocalScripts are client sided you need to create RemoteEvents to send information to server, so basically when you detect the input, fire the RemoteEvent and receive the information then set the speed in the server.

1 Like

I notice your not using LocalPlayer, is this because it is a Script and not a LocalScript. UIS (UserInputService) can only be used in a LocalScript

Oh, @Z_lqify beat me lol.

2 Likes

You don’t need to set speed on server.

1 Like

No you have to do it in Server to make it seem all across the whole server. Or else, it would be only seen by you and it would probably be glitched.

1 Like

No, that’s what you’d think. Character movement (and 99% sure physics in general in roblox, but don’t quote me on that) are automatically replicated across the server

2 Likes

Ah my bad, It does replicate the movements but I don’t think I suggest using this because changing the properties from the client doesn’t replicate in server properties. Which is a bad practice.

2 Likes

Thanks a ton! This cleared up ALOT for me! Here is the script I used:

StarterCharacterScript;

local char = script.Parent
local player = game.Players:GetPlayerFromCharacter(char)

--//

local UserInputService = game:GetService("UserInputService")
local ReplicateStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicateStorage:WaitForChild("RemoteEvent")

--//

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		RemoteEvent:FireServer("ShiftToSprint",{26})
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		RemoteEvent:FireServer("ShiftToSprint",{16})
	end
end)

Then the script which handles RemoteEvent in ServerScriptService;

local repstore = game:GetService("ReplicatedStorage")
local remote = repstore:WaitForChild("RemoteEvent")

remote.OnServerEvent:Connect(function(Player, Type, InfTable)
	if Type == "ShiftToSprint" then
		local Speed = tonumber(InfTable[1])
		local Character = Player.Character
		local Humanoid = Character:WaitForChild("Humanoid")
		
		Humanoid.WalkSpeed = Speed
	end
end)

You’re welcome, glad that I could help.

1 Like

No? As @domboss37 said, these properties replicate to server.

2 Likes

No, the movement does replicate. Not the properties, I just tested out right now.

2 Likes

You need to put keycode I think, and it needs to be a local script inside of StarterGui, or PlayerScripts.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 26
	
	end
end)