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

Help teleporting the player slightly up?

Asked by 8 years ago

Currently, I am trying to teleport the player up by 2 studs. So, I made this script.

name = game.Players.LocalPlayer.Name
char = game.Workspace:FindFirstChild(name)

X = char.Torso.Position.X
Y = char.Torso.Position.Y
Z = char.Torso.Position.Z

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            game.Workspace.Player.Torso.CFrame = CFrame.new(Vector3.new(X,Y+2,Z))
        end)
    end)
end)

I figured that lines 1 and 2 would select the player, lines 4, 5, and 6 would state the X, Y, and Z positions of the torso. The function would simply move the torso up by 2 studs. I however get this error.

**20:34:09.172 - Workspace.Ragdolls.Script:1: attempt to index field 'LocalPlayer' (a nil value)

Help?

0
LocalPlayer ONLY exists in LocalScripts. This causes you to need to remake your variables. alphawolvess 1784 — 8y
0
Thank you. xXWolversXx 10 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

LocalPlayer only works in LocalScripts. You can get the player through the PlayerAdded event so the top part of your script is the main problem, and unnecessary. Also on line 11, we don't need to look for the character because the CharacterAdded function does that for us. The final thing is I would suggest simply Multiplying the character's torso CFrame by CFrame.new(0,2,0) instead of adding two. It will cause the same effect and make your script a little cleaner.

-- Regular Script
game:GetService('Players').PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Died:Connect(function()
            character.Torso.CFrame = character.Torso.CFrame * CFrame.new(0,2,0)
        end)
    end)
end)

Good Luck!

If I helped, please don't forget to accept my answer.
0
Multiplying a CFrame is actually adding them. So the player will still shift up 2 studs however any collisions will not force the player to teleport up until no collision exists, unlike changing Position. alphawolvess 1784 — 8y
0
Just a quick question, if you were teleporting r15, what part would you use instead of torso? xXWolversXx 10 — 8y
0
I think it's still just Torso. If not, then maybe UpperTorso. Experiment. Sorry, I don't use R15 much. User#11440 120 — 8y
Ad

Answer this question