User Input Service not working

Tried to make a simple Shift to sprint script, but as always it didn’t work. Everything is fine (acording to my friend) but UIS doesn’t really respond. Output says nothing, no prints, no errors, walkspeed never changes. It’s a local script placed in StarterPlayerScripts. I think it’s an issue with studio then?

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local walkspeed = player.Character.Humanoid.WalkSpeed
local AverageSpeed = 30
local SprintingSpeed = 48
local debounce = false
local cd = 0.8

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if not debounce then
			debounce = true
			character.Humanoid.WalkSpeed = SprintingSpeed
			task.wait(10)
			for i = 1, (SprintingSpeed - AverageSpeed[walkspeed]) do
				wait(0.1)
				character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed - i
			end
			character.Humanoid.WalkSpeed = 16
			task.wait(cd)
			debounce = false
		end
	end
end)
1 Like

dont use player.Character too early

It can be nil as the script starts

2 Likes

First mistake:

image

Consider using a CharacterAdded event and then call the humanoid because when the script starts the humanoid probably didn’t still loaded and returns nil instead, heres and example:

local character = nil
local walkspeed = nil

player.CharacterAdded:Connect(function() -- Calls the Character when a character is added to the workspace
	character = player.Character
	walkspeed = character.Humanoid.WalkSpeed
end)
player.CharacterRemoving:Connect(function() -- Returns nil to the character when it disappears to the workspace
	character = nil
	walkspeed = nil
end)

Second mistake:

image

I’m not really sure if this is the solution, but you basically used AverageSpeed[walkspeed] to a subtraction, and returns an error because “[]” is usually used to call instances or tables by a value such as a string. Heres my edition which works for me and doesn’t return an error:

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = nil
local walkspeed = nil
local AverageSpeed = 30
local SprintingSpeed = 48
local debounce = false
local cd = 0.8

player.CharacterAdded:Connect(function() -- Calls the Character when a character is added to the workspace
	character = player.Character
	walkspeed = character.Humanoid.WalkSpeed
end)
player.CharacterRemoving:Connect(function() -- Returns nil to the character when it disappears to the workspace
	character = nil
	walkspeed = nil
end)


UIS.InputBegan:Connect(function(input, gameProcessed)
	if character then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			if not debounce then
				debounce = true
				character.Humanoid.WalkSpeed = SprintingSpeed
				task.wait(10)
				for i = 1, (SprintingSpeed - AverageSpeed) do
					wait(0.1)
					character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed - i
				end
				character.Humanoid.WalkSpeed = 16
				task.wait(cd)
				debounce = false
			end
		end
	end
end)

I don’t know why it doesn’t drop you an error to the output, probably a Roblox Studio bug or missconfigurations.

2 Likes

Thanks, it worked. Seems like UIS just bugged, I had to restart studio.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.