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.
02 | local plr = game.Players.LocalPlayer |
03 | local UserInputService = game:GetService( "UserInputService" ) |
04 | local b = (plr.Character or plr.CharacterAdded:Wait()):WaitForChild( 'Torso' ).Position |
05 | UserInputService.InputBegan:Connect( function (input, gameProccesedEvent) |
06 | if input.KeyCode = = Enum.KeyCode.F then |
08 | b = b + Vector 3. new(- 15 , 0 , 0 , 0 ) |
09 | plr.Character.Torso.CFrame = CFrame.new(b) |
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.
02 | local plr = game.Players.LocalPlayer |
03 | local UserInputService = game:GetService( "UserInputService" ) |
04 | UserInputService.InputBegan:Connect( function (input, gameProccesedEvent) |
05 | if input.KeyCode = = Enum.KeyCode.F then |
07 | plr.Character.Torso.CFrame = plr.Character.Torso.CFrame + Vector 3. new(- 15 , 0 , 0 ) |
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:
01 | local ContextActionService = game:GetService( 'ContextActionService' ) |
03 | ContextActionService:BindAction( |
05 | function (Action, State, Input) |
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
02 | local plr = game.Players.LocalPlayer |
03 | local ContextActionService = game:GetService( "ContextActionService" ) |
04 | ContextActionService:BindAction( |
06 | function (Action, State, Input) |
07 | if not plr.Character then return end |
08 | if not plr.Character:FindFirstChild( 'Torso' ) then return end |
10 | if State = = Enum.UserInputState.Begin then |
12 | plr.Character.Torso.CFrame = plr.Character.Torso.CFrame + Vector 3. new(- 15 , 0 , 0 ) |
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?