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

How to make teleportation magic, when you press a key it teleports you to your cursor?

Asked by 4 years ago

So basically I want to make Time Magic when you press the Q key, and then you teleport where you cursor is. I already have the Key part, but how can I make it so that you teleport where your cursor is.

This local script is in StarterCharacterScripts btw. I only have the Key part so far.

0
I can see your new so im gonna say it first to you. this isnt a request site JoyfulPhoenix may of answered but just remember this isnt a request site EnzoTDZ_YT 275 — 4y
0
Yes, Ik, I never asked for the whole script. iiDevPanda 43 — 4y
0
Look things up before you ask on here. Or check out the Dev Forum. ghxstlvty 133 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Using :GetMouse() in a Local Script, you can get the Player's Mouse. In this instance, we can use Mouse.Hit and Mouse.Target. Mouse.Hit returns the CFrame of the Cursor. In simpler terms, where the Mouse is pointing. Mouse.Target returns the object the object the Mouse is pointing to. In this instance, we want to use Mouse.Hit to get the CFrame of the Mouse. When we do that, we can set our HumanoidRootPart's CFrame to the CFrame of the Mouse. It would look something like this.

local Player = game.Players.LocalPlayer -- The Player
local Character = Player.Character or Player.CharacterAdded:Wait() -- The Character
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") -- This is what we will use to teleport the Player
local UserInputService = game:GetService("UserInputService")
local Mouse = Player:GetMouse() -- Getting the Player's Mouse.

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end -- If they are typing in chat then do nothing.
    if input.KeyCode == Enum.KeyCode.Q and Mouse.Hit then -- If Mouse.Hit exists and Player presses Q then. We also added that to check if the Player's Cursor is pointing to the sky which would return nil for obvious reasons.
        local MouseCFrame = Mouse.Hit --Setting a Variable to the Mouse's CFrame (Optional). 
        HumanoidRootPart.CFrame = MouseCFrame -- Set the HumanoidRootPart's CFrame to the Mouse's CFrame meaning teleport the Player to the Mouse.
    end
end)
0
Thanks! iiDevPanda 43 — 4y
0
This honestly really helped! iiDevPanda 43 — 4y
0
Glad to help. JoyfulPhoenix 139 — 4y
Ad

Answer this question