Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Teleport to place on death script is not working?

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?

2 comments

0

Did you try in the game, not studio?

0

Oh, its supposed to be player.Character

1 answer

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!

7 comments

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.

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?

0

check out the revised scripts I wrote!

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.

0

The last code snippet is wrong. It's OnServerEvent

0

Hello?

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.