local a = false local plr = game.Players.LocalPlayer local UserInputService = game:GetService("UserInputService") local b = plr.Torso.position
UserInputService.InputBegan:connect(function(input,gameProccesedEvent) if input.Keycode == Enum.KeyCode.F then print("F") b = b + Vector3.new(-15,0,0,0) local a = true end end)
Making this because I thought the first response was kind sloppy
local player=game.Players.LocalPlayer local char=player.Character --services local UserInput=game:GetService("UserInputService") flag=false UserInput.InputBegan:connect(function(input,gameProcessed) if gameProcessed then return end if input.KeyCode==Enum.KeyCode.C and not flag then flag=true local root=char:FindFirstChild("HumanoidRootPart") if root~=nil then root.Position=Vector3.new(0,0,0) --Position of teleport end end wait(8) flag=false end end)
local a = false local plr = game.Players.LocalPlayer local UserInputService = game:GetService("UserInputService") local b = plr.Torso.position UserInputService.InputBegan:connect(function(input,gameProccesedEvent) if input.Keycode == Enum.KeyCode.F then print("F") b = b + Vector3.new(-15,0,0) local a = true end end)
The reason it doesn't work is because you are declaring the variable B to be the position of the torso located inside the local player, and then you are changing the variable. You are not changing the position of the torso itself. Also, you might want to update your code to use plr.Character.Torso instead of plr.Torso.
Now that we have done that, you might notice that only your torso changes position, and the rest of your body doesn't move along. You can fix this by setting the CFrame of the Torso to a new CFrame instead of a new Vector3.
If you implement all of these changes, your code could look a bit like this.
local a = false local plr = game.Players.LocalPlayer local UserInputService = game:GetService("UserInputService") local b = (plr.Character or plr.CharacterAdded:Wait()):WaitForChild('Torso').Position UserInputService.InputBegan:Connect(function(input, gameProccesedEvent) if input.KeyCode == Enum.KeyCode.F then print("F") b = b + Vector3.new(-15,0,0,0) -- Dunno why you used 4 arguments tbh plr.Character.Torso.CFrame = CFrame.new(b) local a = true end end)
If you paste this in a localscript and test it, you will teleport to a location in mid-air, with the location changing by 15 studs each time you teleport. If I had to guess, this is not your intention, so you can fix this by setting the Torso's CFrame to the Torso's CFrame, and then adding Vector3.new(-15, 0, 0).
Your code should look a bit like this now.
local a = false local plr = game.Players.LocalPlayer local UserInputService = game:GetService("UserInputService") UserInputService.InputBegan:Connect(function(input, gameProccesedEvent) if input.KeyCode == Enum.KeyCode.F then print("F") plr.Character.Torso.CFrame = plr.Character.Torso.CFrame + Vector3.new(-15, 0, 0) local a = true end end)
This can error if the player does not have a character containing a Torso, of a character for that matter, so you can add a simple check before teleporting that will check if the player has a character, and if so, if the character has a torso.
If you paste this in a localscript, it'll perfectly do as expected: each time you press F, you get teleported -15 studs on the X axis. However, if you press F while typing in chat, you will also get teleported. To fix this, we will make use of ContextActionService instead of UserInputService.
We'll start off by calling ContextActionService:BindAction, and we'll give the following parameters: Action name as a string, for example 'Teleport', the function to execute, true/false depending on if you want to create buttons for the action(I believe this is for mobile, but I haven't played around with that yet), and then all the inputs you want.
This might look like this:
local ContextActionService = game:GetService('ContextActionService') ContextActionService:BindAction( 'Teleport', function(Action, State, Input) end, true, Enum.KeyCode.F )
You can mostly reuse the function used for UserInputService, but we need to make a few a change to make it work as expected: ContextActionService activates both when one if the given keys is pressed and released. In this case, if you either press or release F, the function will fire. To solve this, you need to check if State == Enum.UserInputState.Begin. You also won't need to check if Input.KeyCode == Enum.KeyCode.F, so you can just swap these 2.
If you implement all of this, your code should look this this
local a = false local plr = game.Players.LocalPlayer local ContextActionService = game:GetService("ContextActionService") ContextActionService:BindAction( 'Teleport', function(Action, State, Input) if not plr.Character then return end if not plr.Character:FindFirstChild('Torso') then return end if State == Enum.UserInputState.Begin then print("F") plr.Character.Torso.CFrame = plr.Character.Torso.CFrame + Vector3.new(-15, 0, 0) local a = true end end, true, Enum.KeyCode.F )
If you paste this inside a localscript and test it, you'll see that you won't teleport when typing in chat, but you will teleport when pressing F not in chat.
Now that all this is done, there is still 1 thing I'd like to change about this code, and that is the fact that we're using the Torso, and not the HumanoidRootPart. In your own game you might only use R6 characters, but if you want to use this in a different game that does not only have R6 characters it'll simply do nothing when the player has a R15 character. I recommend that you swap out all the Torso's with HumanoidRootPart, and then this should be fixed.
Closed as Non-Descriptive by User#29913, JesseSong, and TaxesArentAwesome
This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.
Why was this question closed?