Help with this script?

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
1 Like

You can just use team spawnpoints. They spawn you randomly by definition. Otherwise use Humanoid.Died event to track death or a player.

1 Like

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.

1 Like

You should actually try it, if it doesn’t go well I’m sorry I’m a newbiešŸ˜…

Sorry but this doesnt work. Thank you a lot for trying to help.

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.

Alright so how would i make it to where they teleport to a random spawn on death?

Sorry if this seems to be a lot to ask, i get confused very easy and i am not a very good scripter.

I believe it would go like this

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)
1 Like

So I’m wondering if it worked

char limit

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 




You have to edit this variables value to the path to the spawn point folder in workspace if there is one

1 Like

Which i did. Lemme rework this again and see what happens.

May I ask what the point of the for-loop is?

Countdown for the code to end and the script to continue.

You know what a for-loop is and what it does, right?

He wants it to execute for a specific period of time. Maybe he can use tick()

How would i use it? I have not used tick before. I am a very bad scripter.

I agree, I would do something like this rather than a for-loop:

local wait_time = 600
local _time = os.clock()

repeat task.wait(1) until os.clock()-_time == wait_time
1 Like

Your code did not work, there are no errors. I have done what you have said to do as well.