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

Help with killing a humanoid?

I'm making a game and I can't seem to figure out how to kill a humanoid. I tried to do many things like spawning in a brick that kills humanoids across a lobby but it wasn't efficent enough. Half of the players won't even die because of the OnTouch detection.

This is the script: (Get ready, it's long.)

--Varibales--
Text = game.Lighting.Text
Maps = {"Testing", "Testing 2"}
NumMaps = 2
--------------

--[[
                                                                                                    
--]]

--------Script--------
---game.Players.NumPlayers < 2

if game.Players.NumPlayers > 1 then
	Text.Value = "Oh no! There is not enough players to continue..."
else
	game.Lighting.isPlayingLobbyMusic.Value = true
	Text.Value = "Welcome to the game! A new game is starting in a couple of seconds..."
	
	wait(10)
	Text.Value = "Picking map..."
	wait(2)
	RandomChoice = math.random(1, NumMaps)
    CurrentMap = Maps[RandomChoice]
    ChosenMap = game.ServerStorage.Maps:FindFirstChild(CurrentMap):clone()
    ChosenMap.Parent = game.Workspace
    Text.Value = "The map selected is : "..CurrentMap.."!"
    wait(3)
    Text.Value = "Preparing your spawn areas..."
    wait(2)
    Text.Value = "Sorting teams..."
    local teamed={}
            local numTeamed=0
            for i,v in pairs(game.Players:GetChildren()) do
                if v.TeamColor==BrickColor.new('Medium stone grey') then
                    numTeamed=numTeamed+1
                end
            end
            for i,v in pairs(game.Players:GetChildren()) do --a loop that goes through all of the players
                if v.TeamColor==BrickColor.new('Medium stone grey') then -- if the player is in trainees team (or on of the teams)
                    local randomposition=math.floor(((tick()*math.random(100))%numTeamed)) -- random number
                    if teamed[randomposition] then
                        repeat randomposition=math.floor(((tick()*math.random(100))%numTeamed)) until not teamed[randomposition]
                    end
                    print(randomposition..' '..v.Name) -- prints that number and name of person
                    teamed[randomposition]=v
                end
            end
            for i,v in pairs(teamed) do
                if i%2==0 then
                    v.TeamColor=BrickColor.new('Bright red')
                elseif i%2==1 then
                    v.TeamColor=BrickColor.new('Bright blue')
                end
            end
    Text.Value = "Sorted! Joining round..."
    game.Lighting.isPlayingLobbyMusic.Value = false
  
    
wait(2)
    
    for i = 40, 0, -1 do
    Text.Value = "Time left : "..i.." seconds"
    wait(1)
end
if Text.Value == "Time left : 0 seconds" then
	Text.Value = "Round over! Well done!"
	
	wait(2)
	Text.Value = "Resetting teams..."
	for i,v in pairs(game.Players:GetChildren()) do
		v.TeamColor = BrickColor.new('Medium stone grey')
	end
	wait(2)
	Text.Value = "Reset teams. Resetting workspace..."
	if game.Workspace:FindFirstChild("Testing") then
		game.Workspace.Testing:remove()
	else
		if game.Workspace:FindFirstChild("Testing 2") then
		game.Workspace["Testing 2"]:remove()
	end
	end
	wait(2)
	Text.Value = "Done! Final preparation..."
	wait(5)
	Text.Value = "Resetting game..."
	wait(2)
	_G:Restart()
end
end

Any help will do. Thanks!

1 answer

✓ Accepted answer
function KillAll()
	for _, v in ipairs(game.Players:GetPlayers()) do -- Get all the players
		spawn(function() -- I like to use spawn functions so if someone leaves at this exact moment the 			script doesn't crash (props KiHeros for teaching me this)
		v:LoadCharacter() -- Reloads the player's character.
		end) -- end spawn function
	end -- end Get the players
end

Using the function KillAll() will respawn all the players if this script is inserted into your script. Basically, what I did with this is get all the players from game.Players, then Load their character.

2 comments

0

There's an error with one of the ('s. I fixed it though

0

Line 2 was missing an 'in ipairs'.

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