Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Shift+T won't teleport to Mouse.Hit.p?

Asked by 8 years ago

Okay, so I am adding a Special Ability, you have to click Shift (48) and T to teleport where your mouse is.

local player = game:GetService("Players").LocalPlayer
mouse = player:GetMouse()
  mouse.KeyDown:connect(function (key)
    if key == 48 and key == "t" then --If 48 isn't shift then sorry..
player.Character:MoveTo(player.Character:MoveTo(mouse.Hit.p))
end
end)

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

The KeyDown event returns the key that was pressed. You're checking if the key that was pressed was Shift and t. That's likes checking if 1 is 1 and 2. 1 will only ever be 1 and 2 will only ever be 2. Therefore, you have to check the values independantly.

But before I get into that then I want to address your usage of KeyDown. KeyDown is deprecated and only exists in the roblox API for compatibility; moreover, so old scripts do not break. You should use UserInputService! UIS is compatible with any and all user input, not just keys.. as well as has a much broader range of keys that can be accessed than KeyUp / KeyDown.. not to mention readability.

So, the UserInputService equivelant of KeyDown would be InputBegan. The InputBegan event returns two values; The input object, and a bool for whether or not the event was fired as a Game Processed Event(GPE). A GPE is something like chatting.. thus, a perfect way to make sure the user is not chatting whilst using this event.

Now.. you would have to have a value that would be true when shift is being pressed and false when not. So, check if the key was Shift with InputBegan to turn it on, and InputEnded to turn it off. Then, you would check if said value was true while the input was the specified key.

--Player
local plr = game.Players.LocalPlayer
--Mouse
local mouse = plr:GetMouse()
--UserInputService
local uis = game:GetService('UserInputService')
--Shift value
local shift = false

--InputBegan
uis.InputBegan:connect(function(input,process)
    --Make sure they're not chatting
    if not process then
        --Check if the key was shift
        if input.KeyCode == Enum.KeyCode.LeftShift then
            --Enable shift value
            shift = true;
        --Otherwise, if it's 'T' and shift is enabled
        elseif input.KeyCode == Enum.KeyCode.T and shift then
            plr.Character:MoveTo(mouse.Hit.p)
        end
    end
end)

--InputEnded
uis.InputEnded:connect(function(input,process)
    if not process then
        if input.KeyCode == Enum.KeyCode.LeftShift then
            shift = false
        end
    end
end)
0
It didn't work. I saw no errors in output, though fireboltofdeath 635 — 8y
0
Worked perfectly for me in studio and in game. Is it in a loclascript in somewhere that replicates to the client? Goulstem 8144 — 8y
0
Starterpack. fireboltofdeath 635 — 8y
0
Allright.. ehmm.. Make sure you're pressing Left Shift, rather than right.. Goulstem 8144 — 8y
View all comments (6 more)
0
I am, I tried both. It says Shift + T in output fireboltofdeath 635 — 8y
0
wat .-. Recopy code plz. You might have pasted.. something else, idk. Goulstem 8144 — 8y
0
Nothing, just says Shift + T fireboltofdeath 635 — 8y
1
Do you happen to have caps lock on? Mystdar 352 — 8y
0
@fireboltofdeath, are you using a computer or a laptop RobotChitti 167 — 8y
0
He accepted it so I guess it worked..? Goulstem 8144 — 8y
Ad

Answer this question