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

i am trying to make a script that when you c press a button you get teleported but it doesnt work? [closed]

Asked by 4 years ago

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)

0
MY EYES AAzterr 27 — 4y

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?

3 answers

Log in to vote
1
Answered by 4 years ago

Making this because I thought the first response was kind sloppy

01local player=game.Players.LocalPlayer
02local char=player.Character
03--services
04local UserInput=game:GetService("UserInputService")
05 
06flag=false
07UserInput.InputBegan:connect(function(input,gameProcessed)
08    if gameProcessed then return end
09    if input.KeyCode==Enum.KeyCode.C and not flag then
10        flag=true
11        local root=char:FindFirstChild("HumanoidRootPart")
12        if root~=nil then
13            root.Position=Vector3.new(0,0,0) --Position of teleport end
14        end
15        wait(8)
16        flag=false
17    end
18end)
Ad
Log in to vote
0
Answered by
Tokyo7979 131
4 years ago
01local a = false
02local plr = game.Players.LocalPlayer
03local UserInputService = game:GetService("UserInputService")
04local b = plr.Torso.position
05 
06UserInputService.InputBegan:connect(function(input,gameProccesedEvent)
07 if input.Keycode == Enum.KeyCode.F then
08print("F")
09b = b + Vector3.new(-15,0,0)
10local a = true
11 end
12 end)
0
If you use Vector3, then it will destroy the player. Use CFrame instead. Also, if a player will chat, and if they press F, then the teleport will be activated, even if they're chat. That's why the gameProcessedEvent exist, and that's why you should've used it. Dovydas1118 1495 — 4y
Log in to vote
0
Answered by 4 years ago

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.

01local a = false
02local plr = game.Players.LocalPlayer
03local UserInputService = game:GetService("UserInputService")
04local b = (plr.Character or plr.CharacterAdded:Wait()):WaitForChild('Torso').Position
05UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
06    if input.KeyCode == Enum.KeyCode.F then
07        print("F")
08        b = b + Vector3.new(-15,0,0,0) -- Dunno why you used 4 arguments tbh
09        plr.Character.Torso.CFrame = CFrame.new(b)
10        local a = true
11    end
12end)

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.

01local a = false
02local plr = game.Players.LocalPlayer
03local UserInputService = game:GetService("UserInputService")
04UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
05    if input.KeyCode == Enum.KeyCode.F then
06        print("F")
07        plr.Character.Torso.CFrame = plr.Character.Torso.CFrame + Vector3.new(-15, 0, 0)
08        local a = true
09    end
10end)

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:

01local ContextActionService = game:GetService('ContextActionService')
02 
03ContextActionService:BindAction(
04    'Teleport',
05    function(Action, State, Input)
06 
07    end,
08    true,
09    Enum.KeyCode.F
10)

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

01local a = false
02local plr = game.Players.LocalPlayer
03local ContextActionService = game:GetService("ContextActionService")
04ContextActionService:BindAction(
05    'Teleport',
06    function(Action, State, Input)
07        if not plr.Character then return end
08        if not plr.Character:FindFirstChild('Torso') then return end
09 
10        if State == Enum.UserInputState.Begin then
11            print("F")
12            plr.Character.Torso.CFrame = plr.Character.Torso.CFrame + Vector3.new(-15, 0, 0)
13            local a = true
14        end
15    end,
16    true,
17    Enum.KeyCode.F
18)

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.