New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Why won't my if key == statements work right?

Asked by 3 months ago

Up is printed whenever ANY key is lifted, and Down is never printed, even when I press the right keys. Does anyone know my problem?

local character = script.Parent
local Mouse = game.Players:GetPlayerFromCharacter(character):GetMouse()
local speed = 8

Mouse.KeyDown:connect(function(key)
    if key == Enum.KeyCode.RightShift then
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed + 8
        print("Shift Down")
    end
    if key == Enum.KeyCode.X then
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed - 9
        print("X Down")
    end
end)

Mouse.KeyUp:connect(function(key)
    if key == Enum.KeyCode.RightShift or Enum.KeyCode.X then
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed
        print("Up")
    end
end)
0
Do not use KeyDown. Use the UserInputService. BashCaster 2370 — 3mo
0
I tried that PaliKai13 92 — 3mo
0
I could not figure it out PaliKai13 92 — 3mo

2 answers

Log in to vote
3
Answered by
Fifkee 1020 Moderation Voter
3 months ago
Edited 3 months ago

Because Mouse.KeyDown returns a string value, if my mind serves me right. InputBegan returns an InputObject with a KeyCode value.

Instead, you should use InputBegan, like so.

game:GetService('UserInputService').InputBegan:Connect(function(KeyPressed, GameProcessedEvent)

    if (not GameProcessedEvent) then --We have this here because if GameProcessedEvent is true, then that means a player is inside of a Gui with modal. We don't want players executing actions while typing or anything like that.

        --I also recommend redefining Key to it's keycode if you're not using any other property (UserInputType and all that)
        KeyPressed = KeyPressed.KeyCode
        if (KeyPressed == Enum.KeyCode.X) then
            xFunctionHere()
        elseif (KeyPressed == Enum.KeyCode.Y) then
            yFunctionHere()
        elseif (KeyPressed == Enum.KeyCode.Z) then
            zFunctionHere()
        end
    end
end)

The Connect function requires a function to call when they signal is fired. We take care of that by making an unnamed new function inside of the first argument. You can also fill in a function name by defining a function outside of the Connect, and giving the proper two arguments.

function Key(Key, Event)
    if (not Event) then
        print('Key pressed!')
    end
end

game:GetService('UserInputService').InputBegan:Connect(Key)

I typically use the first example because I code without thinking (and it works wonders, but I definitely don't recommend it). It looks a little bit less readable than having the actual function outside of the InputBegan, but it still works, and it's a tad bit quicker in my honest opinion.

Good luck!

If this helped, please mark this as the answer and upvote! :^)
Ad
Log in to vote
0
Answered by 3 months ago

I edited your code to fit my needs, and it still doesn't work. Have I done anything wrong? P.S. For some reason it starts me off with 16 speed, then when I lift any key it sets it back to 8, and nothing works again.

local speed = 8

game:GetService('UserInputService').InputBegan:Connect(function(KeyPressed, GameProcessedEvent)
    if not GameProcessedEvent then
        KeyPressed = KeyPressed.KeyCode
        if KeyPressed == Enum.KeyCode.RightShift then
            game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed + 8
        elseif KeyPressed == Enum.KeyCode.X then
            game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed - 9
        end
    end
end)

game:GetService('UserInputService').InputEnded:Connect(function(KeyPressed, GameProcessedEvent)
    if not GameProcessedEvent then
        KeyPressed = KeyPressed.KeyCode
        if KeyPressed == Enum.KeyCode.RightShift or Enum.KeyCode.X then
            game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = speed
        end
    end
end)
0
This is because of your if statement on line 17. You are basically saying "if KeyPressed is RightShift or true" which makes the condition pass. You have to be a little bit redundant with your code, so you'll have to make a comparison to KeyPressed twice. Fifkee 1020 — 3mo
0
Like so: if (KeyPressed == Enum.KeyCode.X or KeyPressed == Enum.KeyCode.Y) then Fifkee 1020 — 3mo

Answer this question