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 finish a script's function?

I'm just starting scripting, but still beginning. I'm working on this script (Pasted Below) but some functions within it aren't working, I could really use help fixing it. If you can, edit the parts that are needing to be fixing, because I'm not sure whats going on with it. Thanks.

THE SCRIPT:

system = script.Parent
is_playing = system.Playing
stage = system.Stage

weapons = game.Workspace.Weapons

hum_team = game.Teams["Humans"]
zom_team = game.Teams["Zombies"]

msg = nil
hint = nil
playing = false
interrupted = false

minutes = 0
seconds = 0

function displayHint(text)
	if(hint==nil)then
		hint = Instance.new("Hint")
		hint.Text = text
		hint.Parent = game.Workspace
	else
		hint.Text = text
	end
end

function removeHint()
	if(hint~=nil)then hint:remove() hint = nil end
end

function displayMsg(text)
	if(msg==nil)then
		msg = Instance.new("Message")
		msg.Text = text
		msg.Parent = game.Workspace
	else
		msg.Text = text
	end
end

function removeMsg()
	if(msg~=nil)then msg:remove() msg = nil end
end

function giveWeapons()
	local player = game.Players:GetChildren()
	if(#player>0)then
		for p = 1, #player do
			if(player[p].TeamColor==hum_team.TeamColor)then
				weapons.Pistol.Value:Clone().Parent = player[p].Backpack
			end
		end
	end
end

function killPlayers()
	local player = game.Players:GetChildren()
	if(#player>0)then
		for p = 1, #player do
			if(player[p].Character~=nil)then
				local h = player[p].Character:FindFirstChild("Humanoid")
				if(h.Health>0)then
					player[p].Character.Humanoid.Health = 0
				else
					if(player[p]:FindFirstChild("Dead")~=nil)then
						player[p].Dead.Value = false
						player[p].Class.Value = "Zombie"
						player[p].TeamColor = hum_team.TeamColor
					end
				end
			end
		end
	end
end

function resetPlayers()
	local player = game.Players:GetChildren()
	if(#player>0)then
		for p = 1, #player do
			if(player[p]:FindFirstChild("Dead")~=nil)then
				player[p].Dead.Value = false
				player[p].Class.Value = "Zombie"
				player[p].TeamColor = hum_team.TeamColor
			end
		end
	end
end

function countPlayers()
	local count = 0
	local player = game.Players:GetChildren()
	if(#player>0)then
		for p = 1, #player do
			if(player[p].Character~=nil)then
				count = count +1
			end
		end
	end
	return count
end

function countPlayersInTeam(team)
	local team_count = 0
	local player = game.Players:GetChildren()
	if(#player>0)then
		for p = 1, #player do
			if(player[p].TeamColor==team.TeamColor)then
				team_count = team_count +1
			end
		end
	end
	return team_count
end

function assignZombies(amount)
	local valid_players = {}
	local player = game.Players:GetChildren()
	if(#player>0)then
		for p = 1, #player do
			if(player[p].TeamColor==hum_team.TeamColor)and
			(player[p].Character~=nil)then
				table.insert(valid_players,player[p])
			end
		end
	end

	if(#valid_players>amount)then
		while(amount>0)do
			local rand = math.random(1,#valid_players)

			local chosen = valid_players[rand]
			chosen.TeamColor = zom_team.TeamColor
			chosen.Character.Humanoid.Health = 0

			table.remove(valid_players,rand)
			amount = amount -1
		end
	end
end

function countDown()
	seconds = seconds -1
	if(seconds<0)then
		minutes = minutes -1
		seconds = 59
		if(minutes<0)then playing = false return true end
	end

	return false
end

function playZombies()
	if(countPlayers()<=3)then
		assignZombies(1)
	elseif(countPlayers()<=6)then
		assignZombies(2)
	else
		assignZombies(3)
	end

	is_playing.Value = true
	displayMsg("Human team, survive the zombie team for 15 minutes to win the game, or else zombies win!")
	giveWeapons(1)
	delay(5,removeMsg)

	interrupted = false
	playing = true

	minutes = 14
	seconds = 60

	while(playing==true)and(countDown()==false)do
		local humans = countPlayersInTeam(hum_team)
		local zombies = countPlayersInTeam(zom_team)

		if(minutes==9)and(seconds==59)then
			stage.Value = 2
			displayMsg("Fast Zombie unlocked! Zombie team, chat '/fastzombie' to play as one next time you respawn!")
			delay(10,removeMsg)
		elseif(minutes==4)and(seconds==59)then
			stage.Value = 3
			displayMsg("Kamikaze Zombie unlocked! Zombie team, chat '/kamizombie' to play as one next time you respawn!")
			delay(10,removeMsg)
		end

		if(seconds<10)then
			displayHint("Humans left: "..humans.." | Time left: "..minutes..":0"..seconds.." | Zombies: "..zombies)
		else
			displayHint("Humans left: "..humans.." | Time left: "..minutes..":"..seconds.." | Zombies: "..zombies)
		end

		if(zombies==0)then -- Zombies left the game? Try to assign another to keep the game running!
			if(countPlayers()>=2)then
				if(countPlayers()<=3)then
					assignZombies(1)
				elseif(countPlayers()<=6)then
					assignZombies(2)
				else
					assignZombies(3)
				end
			else
				interrupted = true
				break
			end
		end

		if(humans==0)then break end
		wait(1)
	end
	stage.Value = 1

	removeHint()

	if(interrupted==true)then
		displayMsg("There are not enough players to continue the game...")
	else
		if(playing==false)then
			displayMsg("Humans have won the match by successfully surviving for 15 minutes!")
		else
			displayMsg("Zombies have won by successfully infecting all humans within 15 minutes!")
		end
	end

	is_playing.Value = false

	killPlayers()
	wait(5)
	resetPlayers()

	removeMsg()
end

while true do
	wait(3)
	resetPlayers()
	if(countPlayers()>=2)then
		removeMsg()
		playZombies()
	else
		displayMsg("Waiting for two or more players...")
	end
end

1 comment

0

May you give the output of when the code is ran

1 answer

On line 43, you never closed the if statement with 'end', you only closed the function

function removeMsg()
	if(msg~=nil)then msg:remove() msg = nil end
	end
end

1 comment

0

He did close the function; check the line that has the conditional. >->

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