Hi! I need to figure out a way to make this script function only when a player dies, for a certain amount of time. Everytime a player dies that SAME player gets respawned at a random location. It seems to work for the respawn part but it repeats every second, (it should because thats how its scripted.) So my problem is i cant seem to figure out how to make this function for that amount of time and how to make it where if a player dies that player gets respawned at a random location. Any help is appreciated, the script is in ServerScriptService.
local players = game:GetService("Players")
function TeleportPlayers(map)
local teleportpads = map:WaitForChild("PlayerSpawns"):GetChildren()
for i, player in pairs(players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
character:PivotTo(teleportpads[math.random(1,#teleportpads)].CFrame)
end
end
for i = 1,600 do
task.wait(1)
end
Yes i would like to use the humanoid died event to track when a player dies, then the same player gets respawned at a random location. Also i find it easier to use this, ye i dont know how to script it in correctly.
for i = 1,600 do
humanoid.Died:Connect(function()
local spawns = spawns:GetChildren()
Player:LoadCharacter()
hrp:MoveTo(spawns[math.random(1, #spawns).p)
task.wait(1)
end
I didnāt test these as Iām computerlessā¦
Also errors can happen because I have only started learning scripting. And itās been two months.
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
task.spawn(function()
-- stuff you want to do when the player dies
end)
end)
end)
end)
Sorry for the bad formatting, Iām on mobile right now.
local spawns = path.to.spawns -- spawns folder
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")-- yeilds/stops the script until the humanoid is loaded
humanoid.Died:Connect(function()
task.spawn(function() -- spawns in a new "thread"
local rand = Random.new():NextInteger(1,#spawns:GetChildren()) -- Makes a random number
local randspawn = spawns:GetChildren()[rand]
character:PivotTo(CFrame.new(randspawn.Position+Vector3.new(0,10,0)))-- positions the character on the spawn
end)
end)
end)
end)
It for some reason did not work for me? here is the way i work with my random spawns, maybe you can take from this:
local players = game:GetService(āPlayersā)
function TeleportPlayers(map)
local teleportpads = map:WaitForChild("PlayerSpawns"):GetChildren()
for i, player in pairs(players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
character:PivotTo(teleportpads[math.random(1,#teleportpads)].CFrame)
end
end
and here is the full script:
-- Player Countdown
while true do
local availableplayers = {}
repeat
availableplayers = {}
-- Checks if player is In the menu, and inserts only available players.
for i, plr in pairs(game.Players:GetPlayers()) do
if not plr:FindFirstChild("InMenu") then
table.insert(availableplayers, plr)
end
end
task.wait(3)
until #availableplayers >= 1
wait(15)
warn("Starting game up as players neccessary is here.")
wait(3)
warn("Choosing random map!")
-- Choosing a map
local Maps = game.ServerStorage.Maps:GetChildren()
local ChosenMap = Maps[math.random(1, #Maps)]
local ChosenMapB = ChosenMap:Clone()
ChosenMapB.Parent = workspace
wait(0.1)
warn("Map Chosen: "..ChosenMapB.Name)
-- Announcement to players that the game will start soon.
local ReplicatedStorage = game.ReplicatedStorage
local event = ReplicatedStorage.Announcement
event:FireAllClients()
wait(5)
local event2 = ReplicatedStorage.CallOfAnnouncement
event2:FireAllClients()
wait(3)
-- Teleporting Players to game.
local players = game:GetService("Players")
function TeleportPlayers(map)
local teleportpads = map:WaitForChild("PlayerSpawns"):GetChildren()
for i, player in pairs(players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
character:PivotTo(teleportpads[math.random(1,#teleportpads)].CFrame)
end
end
for i = 1,600 do
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
task.spawn(function()
end)
end)
end)
end)
task.wait(1)
end
-- Intiating Round Cleanup
ChosenMapB:Destroy()
wait(5)
end