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

What is wrong with this script?

I was hoping that this script would make is so the time would start counting down again every time the round restarted(countdown function) and then if there were no survivors it would do the infected_finish function and if time ran out it would do the survivor_finish.

function countdown()
    time_left=(900)
end
while true do
    time_left=time_left-1
wait(1)
end

countdown()
 
 
function infected_finish(text,time)
if game.Teams.Survivors:GetPlayers()== nil then
    m=Instance.new("Message",Workspace)
    m.Text = text
    wait(time)
    m:Destroy()
for i,v in pairs(game.Players:GetPlayers()) do
    v.Character.Humanoid.Health = 0
    time_left=900
    end
end
end
 
 
 
function survivor_finish(text,time)
    if game.Teams.Survivors:GetPlayers()~= nil and time_left==0 then
    m=Instance.new("Message",Workspace)
    m.Text = text
    wait(time)
    m:Destroy()
    end

    for i,v in pairs(game.Players:GetPlayers()) do
    v.Character.Humanoid.Health = 0
    time_left=900
    end
 
infected_finish("The final survivor has been killed. Infected win!",3)
survivor_finish("Time has run out,survivors win!",3)
end

Thanks

1 comment

0

Anything after a while function isn't ran. Place the While loop at the bottom.

1 answer

✓ Accepted answer

One) Main Loop Placement

Lua is a procedural scripting language (on the whole). This means it executes in direct order.

The while loop from lines 04 to 07 will never stop. This means the function declarations below it will never be run.

The main loop must occur after all declarations.


Two) Tab & Space properly

Tab and space your code properly. It makes it much easier to read and understand.

time_left=(900) should be time_left = 900.

infected_finish should look like this:

-- NOTE THAT THE FOLLOWING CODE INCORRECT
-- AND IS BEING QUOTED FOR STYLISTIC PURPOSES
-- ONLY
function infected_finish(text,time)
	if game.Teams.Survivors:GetPlayers() == nil then
		m = Instance.new("Message", Workspace)
		m.Text = text
		wait(time)
		m:Destroy()
		for i, v in pairs(game.Players:GetPlayers()) do
			v.Character.Humanoid.Health = 0
			time_left = 900
		end
	end
end

Three) GetPlayers

Team objects do not have a GetPlayers() method.

Furthermore, if they did, it would never return nil. That would be a horrible design. A list would be empty, that is, have a length (# operator) of 0.

Here is a function you could use to get the players on a team:

function getTeam(team)
	local players = game.Players:GetPlayers();
	local teamPlayers = {};
	for _, player in pairs(players) do
		if player.TeamColor == team.TeamColor then
			table.insert(teamPlayers, player);
		end
	end
	return teamPlayers;
end

3 comments

0

cool that makes sense thanks

0

how do I use this though?

0

if #getTeam(game.Teams.Survivor) == 0 and time_left == 0 then for line 28's condition; and so this question can be finished please mark it as accepted

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