Search code examples
luarobloxluau

Script in roblox is not working and doesn't have any error in the output


I was trying to make a system that makes the player run by the humanoid walkspeed, but it didn't work. Firstly the code was returning an error that said Attempt to index nil with Humanoid or the player Character. I don't know what's happening. Code below.

local Player = game.Players.LocalPlayer

local UserInputService = game:GetService("UserInputService")

task.wait(5)

local Character = Player.Character
local Humanoid = Character.Humanoid

local Running = false

UserInputService.InputBegan:Connect(function(input, typing)
    if input.UserInputType == Enum.UserInputType and typing == false then
        if input.KeyCode == Enum.KeyCode.LeftControl then
            if Running == false then
                Running = true
                Humanoid.WalkSpeed += 16
            end
        end
    end
end)

UserInputService.InputBegan:Connect(function(input, typing)
    if input.UserInputType == Enum.UserInputType and typing == false then
        if input.KeyCode == Enum.KeyCode.LeftControl then
            if Running == true then
                Running = false
                Humanoid.WalkSpeed = 16
            end
        end
    end
end)

Solution

  • The problem is pretty simple: You're comparing input.UserInputType with Enum.UserInputType. These will never be equal. To better understand why, here is some info about how enums work.

    Enums

    The simplest definition is that an enum contains possibilities. For example, Enum.KeyCodes represent whatever key you've pressed. Or, Enum.Material contains all materials a part can have.

    You seem to have confused the enum types with enum items.

    Enums contain possibilities, or enumerations. An enum's items are these enumerations. Enum items are these enumerations, each corresponds to one.

    You are trying to compare an enum item to the entire thing that holds all enumerations. These are not the same. This is like saying that a part is the workspace. That's not the case, the workspace holds the part, but it isn't the part.