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

I'm having trouble with a click to teleport script, can anyone help?

Asked by 6 years ago

Local Script

1print(1)
2local mse = game.Players.LocalPlayer:GetMouse()
3print(2)
4mse.Button1Down:connect(function(plr)
5    print(3)
6    plr.Character.HumanoidRootPart.Position = Vector3.new(mse.Hit.X, mse.Hit.Y, mse.Hit.Z)
7    print(4)
8end)

2 answers

Log in to vote
1
Answered by 6 years ago

Button1Down doesn't pass the player. Use the LocalPlayer property of Players, and use UserInputService instead.

01local offset = CFrame.new(0, 3, 0)
02local client = game:GetService("Players").LocalPlayer
03local mouse = client:GetMouse()
04local UserInputService = game:GetService("UserInputService")
05 
06UserInputService.InputBegan:Connect(function(input, gpe)
07    if gpe then return end -- interacted with gui
08 
09    if input.UserInputType == Enum.UserInputType.MouseButton1 then
10        client.Character:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.Position) * offset)
11    end
12end)

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


Don't forget to accept if this helps

Ad
Log in to vote
0
Answered by
Imperialy 149
6 years ago
01local p = game.Players.LocalPlayer
02local UIS = game:GetService("UserInputService")
03local mouse = p:GetMouse()
04 
05function GetCharacter() -- gets the character
06    return p.Character
07end
08 
09function Teleport(position)
10    local Char = GetCharacter()
11    if Char then
12        Char:MoveTo(position) -- moveto mouse.Hit
13    end
14end
15 
View all 21 lines...

this 1 is better because you can hold down a key and click to teleport just hold down e

Answer this question