If you die, you rejoin

Hello,

I have a thing when your character dies, the whole script gets bugged and nobody knows why.
I always have to say rejoin.

Can someone help me: If your character dies ingame: You rejoin the game.

Sincerely,
Milk

3 Likes

the code you wrote would help a lot

3 Likes
local players = game:GetService("Players")

local DeathMessage = "You Died! Better luck next time!"

players.PlayerAdded:Connect(function(player)
    player.CharacterAppearanceLoaded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")

        humanoid.Died:Connect(function()
           player:Kick(deathMessage)
       end)
    end)
end)
2 Likes

Could it be that your script is a local script and its in starterpack? If that’s the case then you should try moving it to startercharacterscripts or cloning the script on death.

Player.CharacterRemoving:Connect(function(Character)
	local scriptClone = script:Clone()
	scriptClone.Parent = game.ReplicatedStorage
	Player.CharacterAdded:Wait()
	scriptClone.Parent = Player.Backpack
end)
1 Like

It sounds like something else is going on that needs to be looked into. However, to answer the question at this time, you could re-teleport the player back into the game after they die.

local players = game:service("Players")
local ts = game:service("TeleportService")

players.PlayerAdded:connect(function(plr)
    local char = plr.Character
    if char == nil then char = plr.CharacterAdded:wait() end
    char:waitForChild("Humanoid").Died:connect(function()
        ts:Teleport(game.PlaceId, plr)
    end)
end)
2 Likes

This is an actual problem that needs resolving.

However, I’ll also solve the point of the post:

--Script in StarterCharacterScripts
local TeleportService = game:GetService("TeleportService")
local RunService = game:GetService("RunService")

local Character = script.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

function otherKick()
	warn("Player can't rejoin!")
	Player:Kick("You died!")
end

TeleportService.TeleportInitFailed:Connect(function(p, result, err, id)
	if p ~= Player or id ~= game.PlaceId then return end
	TeleportService:TeleportAsync(game.PlaceId, {Player})
end)

Humanoid.Died:Once(function()
	if RunService:IsStudio() then
		otherKick()
	else
		TeleportService:TeleportAsync(game.PlaceId, {Player})
	end
end)

PS: I’m pretty sure there’s a maximum of how many times you can teleport a player(to prevent botting of visits), but I don’t remember the erroring of that. Whoever finds the error specific to running out of teleports, just write a condition for it(in the TeleportInitFailed function), and if it’s true run the otherKick() method instead of the teleport function.

excuse me? did you just write game:service()

game.Players.PlayerAdded:Connect(function(player)
   player.CharacterAdded:Connect(function(character)
      character.Humanoid.Died:Connect(function()
         local TO = Instance.new("TeleportOptions")
         TO.ServerInstanceId = game.JobId
         TO.Parent = workspace
         game:GetService("TeleportService"):TeleportAsync(game.PlaceId, {player}, TO)
         TO:Destroy()
         TO = nil
      end)
   end)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.