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

How do i make a player teleport if hes dead?

Asked by 5 years ago

Hello, i've recently been trying to get into scripting and as i am new to it, i don't understand most things. I've been trying to make a script that teleports you to a **XYZ ** position and sets your walkspeed to 0 when you die. I really don't understand what i did wrong i've searched the internet but the best thing i can do is just ask here.


game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
        Humanoid.Walkspeed = 0
        end)
    end)
end)

function Teleport()

x = target.-23.56

y = target.0.5

z = target.-0.615

game.Players.LocalPlayer.Character:MoveTo(Vector3.new(-23.56,0.5,0.615))

        end)
    end)
end)

if Humanoid.Walkspeed = 0 then Teleport()

I know i've messed up a lot of things in this, but i am looking for help. Thank you for your attention.

0
what do you mean by "teleport when dead"? like when they die you want them to teleport to a position and then they respawn User#23365 30 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

The logic you are displaying is promising. However, there are some issues that I will try to resolve in my answer. It appears you are using a server script which cannot access the LocalPlayer. You can teleport the player inside of the .Died event. Because the script runs all the way through at the beginning of the game the if statement will only check once. Also, it is not a really efficient way to check and see if they died. I do think you are on the right track. If you create variables for x, y, and z you should use those variables instead of typing out the values all over again. After all my comments this is kind of what I recommend:


local players = game:GetService('Players') -- I always create a variable in case I need to use it again players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function() -- Use :Connect, :connect is deprecated and should not be used local humanoid = character:WaitForChild('Humanoid') -- just a variable so I can use the humanoid again when I want to teleport the player humanoid.Died:Connect(function() humanoid.HumanoidRootPart.CFrame = CFrame.new(x, y, z) -- the x, y, and z that you choose end) end) end)

I hope this helps and have a great day scripting!

0
Please ignore my messy grammar and sentences. User#21908 42 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

So there are a few things wrong with your script, Let's break it down:

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
        Humanoid.Walkspeed = 0
        end)
    end)
end)

This part is fine, but the only thig you change is "Walkspeed" to WalkSpeed" with capital S.

function Teleport()

x = target.-23.56

y = target.0.5

z = target.-0.615

game.Players.LocalPlayer.Character:MoveTo(Vector3.new(-23.56,0.5,0.615))

        end)
    end)
end)

The first problem with this part is the x,y,z variables, what is "target."? If you want to make a varibale with a position, I suggest you doing something like so; Pos = Vector3.new(0,0,0) . And assuming this is in a normal Script, you can't use game.Players.LocalPlayer. That only works in a LocalScript. And another thing is that you dont need all those ends. Just one end without the ")".

if Humanoid.Walkspeed = 0 then Teleport()

And this part is really unneeded, you can just call the function Teleport() inside the Humanoid.Died Event. And to check if something is equal to 0, you would do == not =. I suggest reading this.

Here is a fixed version of your script:

game:GetService('Players').PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
        player.Character.Humanoid.WalkSpeed = 0
        Teleport(player) -- Calling the function Teleport() and sending the Player who died to the Function
        end)
    end)
end)

function Teleport(player) --  Now we got the Player that died.
player.Character:MoveTo(Vector3.new(-23.56,0.5,0.615))
end

If this didn't make any sense, let me know so I can try to explain some more.

1
Thank you both, i think i found my answer. Thanks again in advance. xiisuma 3 — 5y

Answer this question