I'm trying to teleport a player ~50 studs in front of where the camera is looking at, whenever they double tap the "w" key. I know how to teleport the player and find the direction of the camera (At least I think I'm doing it right, I could be wrong), but I can't seem to put them together without getting the same error:
15:33:11.773 - Players.McQuakyDuck.PlayerGui.LocalScript:37: bad argument #2 to '?' (Vector3 expected, got number)
All the code is in a LocalScript inside of StarterGui
--Getting the Camera game.Players.PlayerAdded:connect(function(player) script.LocalScript:clone().Parent = player.CharacterAdded:wait() end) --Local Variables local cam = workspace.CurrentCamera local player = game.Players.LocalPlayer game.Workspace:WaitForChild(player.Name) local Char = player.Character local Humanoid = Char:WaitForChild("Humanoid") local Mouse = player:GetMouse() local lastTapTime = 0 local clicked = false local Trail = game.Workspace.Trail:Clone() _G.Energy = 100 local Mouse = player:GetMouse() --Moving the Humanoid --Double Tap enabled = true Mouse.KeyDown:connect(function(key) if not enabled and clicked then return end if key == "w" and _G.Energy >= 25 then enabled = false local function onTap() if tick() - lastTapTime < 0.2 then print("double Click!") clicked = true --What happends when the user double taps player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3(cam.CFrame.lookVector + 50,cam.CFrame.lookVector + 50,cam.CFrame.lookVector + 50)) --player.Character.UpperTorso.Velocity = cam.CFrame.lookVector * Vector3.new(1,0,1) * 400 Trail.Parent = Char.HumanoidRootPart local attachment0 = Instance.new("Attachment", Char.Head) attachment0.Name = "TrailAttachment0" local attachment1 = Instance.new("Attachment", Char.HumanoidRootPart) attachment1.Name = "TrailAttachment1" Trail.Attachment0 = attachment0 Trail.Attachment1 = attachment1 wait(0.3) Trail:Remove() --_G.Energy = _G.Energy - 25 wait(0.5) end end onTap() lastTapTime = tick() wait(0.5) enabled = true clicked = false end end)
Any help is appreciated!
You are trying to add a number to a lookVector, which is a Vector3, and they can’t be added together. You can only add a Vector3 to a Vector3 or a Vector3 to a CFrame. So, to correctly do this, you need to add the lookVector to another Vector3, not a number. And because lookVector is already a Vector3, you do not need to use Vector3.new()
. Here’s how it’s correctly done:
I’d assume you want to add 50 studs on each axis:
--Getting the Camera game.Players.PlayerAdded:connect(function(player) script.LocalScript:clone().Parent = player.CharacterAdded:wait() end) --Local Variables local cam = workspace.CurrentCamera local player = game.Players.LocalPlayer game.Workspace:WaitForChild(player.Name) local Char = player.Character local Humanoid = Char:WaitForChild("Humanoid") local Mouse = player:GetMouse() local lastTapTime = 0 local clicked = false local Trail = game.Workspace.Trail:Clone() _G.Energy = 100 local Mouse = player:GetMouse() --Moving the Humanoid --Double Tap enabled = true Mouse.KeyDown:connect(function(key) if not enabled and clicked then return end if key == "w" and _G.Energy >= 25 then enabled = false local function onTap() if tick() - lastTapTime < 0.2 then print("double Click!") clicked = true --What happends when the user double taps player.Character.HumanoidRootPart.CFrame = CFrame.new(cam.CFrame.lookVector + Vector3.new(50, 50, 50)) --player.Character.UpperTorso.Velocity = cam.CFrame.lookVector * Vector3.new(1,0,1) * 400 Trail.Parent = Char.HumanoidRootPart local attachment0 = Instance.new("Attachment", Char.Head) attachment0.Name = "TrailAttachment0" local attachment1 = Instance.new("Attachment", Char.HumanoidRootPart) attachment1.Name = "TrailAttachment1" Trail.Attachment0 = attachment0 Trail.Attachment1 = attachment1 wait(0.3) Trail:Remove() --_G.Energy = _G.Energy - 25 wait(0.5) end end onTap() lastTapTime = tick() wait(0.5) enabled = true clicked = false end end)