Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

10 comments

0

It didn't work. I saw no errors in output, though

0

Worked perfectly for me in studio and in game. Is it in a loclascript in somewhere that replicates to the client?

0

Starterpack.

0

Allright.. ehmm.. Make sure you're pressing Left Shift, rather than right..

0

I am, I tried both. It says Shift + T in output

0

wat .-. Recopy code plz. You might have pasted.. something else, idk.

0

Nothing, just says Shift + T

1

Do you happen to have caps lock on?

0

@fireboltofdeath, are you using a computer or a laptop

0

He accepted it so I guess it worked..?

This discussion is preserved as it was. New answers, comments, and votes are closed.