Local Script
1 | print ( 1 ) |
2 | local mse = game.Players.LocalPlayer:GetMouse() |
3 | print ( 2 ) |
4 | mse.Button 1 Down:connect( function (plr) |
5 | print ( 3 ) |
6 | plr.Character.HumanoidRootPart.Position = Vector 3. new(mse.Hit.X, mse.Hit.Y, mse.Hit.Z) |
7 | print ( 4 ) |
8 | end ) |
Button1Down doesn't pass the player. Use the LocalPlayer
property of Players, and use UserInputService instead.
01 | local offset = CFrame.new( 0 , 3 , 0 ) |
02 | local client = game:GetService( "Players" ).LocalPlayer |
03 | local mouse = client:GetMouse() |
04 | local UserInputService = game:GetService( "UserInputService" ) |
05 |
06 | UserInputService.InputBegan:Connect( function (input, gpe) |
07 | if gpe then return end -- interacted with gui |
08 |
09 | if input.UserInputType = = Enum.UserInputType.MouseButton 1 then |
10 | client.Character:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.Position) * offset) |
11 | end |
12 | end ) |
I use :SetPrimaryPartCFrame()
, as Vector3 factors in collision, and CFrame does not. I just create a CFrame positioned at mouse.Hit.Position
and multiply the CFrame by offset
so they don't get stuck in the ground
01 | local p = game.Players.LocalPlayer |
02 | local UIS = game:GetService( "UserInputService" ) |
03 | local mouse = p:GetMouse() |
04 |
05 | function GetCharacter() -- gets the character |
06 | return p.Character |
07 | end |
08 |
09 | function Teleport(position) |
10 | local Char = GetCharacter() |
11 | if Char then |
12 | Char:MoveTo(position) -- moveto mouse.Hit |
13 | end |
14 | end |
15 |
this 1 is better because you can hold down a key and click to teleport just hold down e