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

How do i make a last man standing script?

I already made game rounds but if there only one player survived the game will still go untill it ends. local player = game.Players.LocalPlayer

Winners = {}

Settings = require(script.Settings)

Intermission = Settings.Intermission GameTime = Settings.GameTime

function ChangeText(Text) local Players = game.Players:GetPlayers() for i = 1, #Players do Players[i].PlayerGui.Main.Time.Text = Text end end

function GetMap() local Maps = game.ReplicatedStorage.Maps:GetChildren() local PickMap = math.random(1, #Maps) local SelectedMap = Maps[PickMap]:Clone() SelectedMap.Name = 'Map' SelectedMap.Parent = game.Workspace end

function RemoveMap() if game.Workspace:FindFirstChild('Map') then game.Workspace.Map:remove() end end

while true do if game.Players.NumPlayers >= 2 then for i = Intermission, 0, -1 do ChangeText('Intermission ' ..i) wait(1) end

local Players = game.Players:GetPlayers()
for i = 1, #Players do
	Players[i].Playing.Value = true
end

ChangeText('Game starting...')
wait(2)
GetMap()
wait(1)

local Players = game.Players:GetPlayers()
for i = 1, #Players do
	if Players[i].Playing.Value == true and Players[i].isAFK.Value == false then
		local Spawn = game.Workspace.Map.Spawns.Spawn
		Players[i].Character.UpperTorso.CFrame = CFrame.new(Spawn.Position)

		local Sword = game.ReplicatedStorage.Sword
		Sword:Clone().Parent = Players[i].Backpack
	
		
		Spawn:remove()
		Players[i].Character.Humanoid.WalkSpeed = 0
	end
end

for i = 5, 0, -1 do
	ChangeText('Starting in  ' ..i)
	wait(1)
end	

local Players = game.Players:GetPlayers()
for i = 1, #Players do
	if Players[i].Playing.Value == true then
		Players[i].Character.Humanoid.WalkSpeed = 16
	end
end

ChangeText('BEGIN!')

for i = GameTime, 0, -1 do
	ChangeText('GameTime ' ..i)
	wait(1)
end


local Players = game.Players:GetPlayers()
for i = 1, #Players do
	if Players[i].Playing.Value == true then
		table.insert(Winners, Players[i].Name)
		Players[i].leaderstats.Money.Value = Players[i].leaderstats.Money.Value + 5
		Players[i].leaderstats.Wins.Value = Players[i].leaderstats.Wins.Value + 1
		Players[i].Character.Humanoid.Health = 0
		Players[i].Playing.Value = false
		
		Players[i].PlayerGui.Main.MoneyShower.TextLabel.Visible = true
	end
end

RemoveMap()
ChangeText('GAME OVER!')
wait(4)	

end end

1 answer

This isn't too hard to implement; you'll need a couple of things. One of these is a "playersingame" variable.

Take a look at this example:

local playersingame = {};

-- a utility function to remove a player from playersingame by value, rather than key
local removeplayer = function(player)
	for a, x in pairs(playersingame) do
		if(x == player)then
			table.remove(playersingame, a);
		end
	end
end

-- round start
for a, x in pairs(game.Players) do
	-- insert all players into the game
	table.insert(playersingame, x);
end

-- register a function to be called by any player in-game who dies
for a, x in pairs(playersingame) do
	local death;
	death = x.Character.Humanoid.Died:connect(function()
		death:disconnect();
		x.Team = game.Teams.Out;
		removeplayer(x);
	end)
end

-- ... do whatever you have to do to setup the game

-- main game loop
while wait() do
	-- since this is the game loop, we should check whether more than 1 player is left
	-- if not, then the game is over
	if(#playersingame == 1)then
		print("Round is over! " .. playersingame[1].Name .. " won!");
		playersingame = {};
		break;
	elseif(#playersingame < 1)then
		print("Round is over! It's a draw!");
		playersingame = {};
		break;
	end
	-- if we make it to this point, the game is still ongoing
	-- do game stuff
end

-- round ended; reset the script by reenabling it from another script (or rewrite this code as a function)
script.Disabled = true;

2 comments

0

this is my game script: repeat wait(1) until game.Players.NumPlayers >= 2 local player = game.Players.LocalPlayer Winners = {} Settings = require(script.Settings) Intermission = Settings.Intermission GameTime = Settings.GameTime function ChangeText(Text) local Players = game.Players:GetPlayers() for i = 1, #Players do Players[i].PlayerGui.Main.Time.Text = Text end end function GetMap

0

Edit your question with the code posted in Lua tags, and I can try and help you later.

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