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

Teleport to place on death script is not working?

Asked by 5 years ago

I want to make it so that when the localplayer dies they are teleported to another game, so I made a localscript and inserted it into ReplicatedFirst and wrote this:

local TeleportService = game:GetService("TeleportService")
local gameID = 2094965912
local player = game.Players.LocalPlayer

player:WaitForChild("Humanoid").Died:connect(function()
    TeleportService:Teleport(gameID, player)
end)

However, nothing happened. Can anyone please tell me what I'm doing wrong?

0
Did you try in the game, not studio? mixgingengerina10 223 — 5y
0
Oh, its supposed to be player.Character mixgingengerina10 223 — 5y

1 answer

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

Hi there! "Humanoid" is located in the players character, so here's what the "player" variable should be:

local player = game.Players.LocalPlayer.Character

If this doesn't work, it may be because the server takes a while to load the character. Here's an alternative script, should the first one not work:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
                TeleportService:Teleport(gameID, player)
        end)
    end)
end)

There ya are! :D

REVISED:

local script:

local player = game.Players.LocalPlayer.Character

player:WaitForChild("Humanoid").Died:connect(function() 
game.ReplicatedStorage.RemoteEvent:FireServer(gameID)
end)

Server script

game.ReplicatedStorage.RemoteEvent.OnServerInvoke:Connect(function(player,gameID)
    TeleportService:Teleport(gameID, player)
end)

Remember that the player argument is given in remote events and functions!

0
Thank you for the response, but it teleported the player I was testing with who was alive instead of me right after I reset. TrixitesWasTaken 13 — 5y
0
Whoops, the script I wrote is server sided, not client sided. Maybe create a RemoteEvent that the local script fires, and a script in ServerScriptService receives? SnazzySpider67 53 — 5y
0
check out the revised scripts I wrote! SnazzySpider67 53 — 5y
0
Thank you yet again for this, but how exactly would I use the RemoteEvents? I'm sorry but I'm just really confused when it comes to RemoteEvents as I have little experience with them. TrixitesWasTaken 13 — 5y
View all comments (3 more)
0
The last code snippet is wrong. It's OnServerEvent User#19524 175 — 5y
0
Hello? TrixitesWasTaken 13 — 5y
0
Thanks for catching that, Incapaz, It's harder for me to code if I can't test anything. Try and look up a video to learn about remote events and functions. As much as I can tell you, a remote event can be created anywhere where both the client and server can access it, such as ReplicatedStorage, and can be used for a client to send a message to the server, or vice versa. SnazzySpider67 53 — 5y
Ad

Answer this question