-- connect events
-----------------------------------------------------------------------------------------------------------------------

function unequip()
	local items=script.Parent:children()
	for i=1, #items do
		if items[i].className=="Tool" then items[i]:remove() end
	end
end

function onChatted(msg, recipient) 
	msg = string.lower(msg)

	if string.match(msg, string.lower(script.Parent.Name))~=nil or string.match(msg, "everyone") then
		if string.match(msg, "equip") then 
			if game.Workspace:findFirstChild("Hub") then
				if string.match(msg, "rocket") then unequip()
					game.Workspace.Hub.Rocket:clone().Parent=script.Parent
				elseif string.match(msg, "slingshot") then unequip()
					game.Workspace.Hub.Slingshot:clone().Parent=script.Parent
				elseif string.match(msg, "sword") then unequip()
					game.Workspace.Hub.Sword:clone().Parent=script.Parent
				elseif string.match(msg, "pbg") then unequip()
					game.Workspace.Hub.PBG:clone().Parent=script.Parent
				elseif string.match(msg, "superball") then unequip()
					game.Workspace.Hub.Superball:clone().Parent=script.Parent
				elseif string.match(msg, "trowel") then unequip()
					game.Workspace.Hub.Trowel:clone().Parent=script.Parent
				elseif string.match(msg, "bomb") then unequip()
					game.Workspace.Hub.Bomb:clone().Parent=script.Parent
				end
			end
		end
		if string.match(msg, "unequip") then unequip() end
		if string.match(msg, "run") then onRunning(1) end
		if string.match(msg, "climb") then onClimbing() end
		if string.match(msg, "jump") then onJumping() end
		if string.match(msg, "zombie") then pose="Zombie" end
		if string.match(msg, "disco") then pose="Boogy" end
		if string.match(msg, "float") then pose="Float" end
		if string.match(msg, "punch") then pose="Punch" end
		if string.match(msg, "kick") then pose="Kick" end
		if string.match(msg, "fly") then pose="Fly" end
		if string.match(msg, "heal") then script.Parent.Humanoid.Health=script.Parent.Humanoid.MaxHealth end
		if string.match(msg, "defend") then defence() end
		if string.match(msg, "stop") then pose="Standing"; proxkill=false; following=false; stopmoving() end
		if string.match(msg, "go home") then following=false; gohome() end
		if string.match(msg, "follow") then
			if string.match(msg, "all") then
				followany()
			else
				local egg=game.Players:children()
				for i=1, #egg do
					if string.match(msg, string.lower(egg[i].Name)) then
						follow(egg[i].Name)
						return
					end
				end
			end
		end
		if string.match(msg, "kill") then
			if string.match(msg, "all") then
				attackany()
			else
				local egg=game.Players:children()
				for i=1, #egg do
					if string.match(msg, string.lower(egg[i].Name)) then
						attack(egg[i].Name)
						return
					end
				end
			end
		end

	end
end

if game.Players.NumPlayers>1 then
	x=game.Players:children()
	
	for i=1, #x do
		if script.Parent:findFirstChild("Commander")~=nil then
			if script.Parent.Commander:children()~=nil or script.Parent.Commander:children()>0 then
				local ch=script.Parent.Commander:children()
				for i=1, #ch do
					if string.lower(ch[i].Name)==string.lower(x[i].Name) then
						x[i].Chatted:connect(function(msg, recipient) onChatted(msg, recipient) end) 
					end
				end
			elseif string.lower(script.Parent.Commander.Value)==string.lower(x[i].Name) then
				x[i].Chatted:connect(function(msg, recipient) onChatted(msg, recipient) end) 
			end
		else
			x[i].Chatted:connect(function(msg, recipient) onChatted(msg, recipient) end)
		end
	end
end


function onPlayerEntered(Player) 
	while Player.Name==nil do
		wait(2)
	end
	if script.Parent:findFirstChild("Commander")~=nil then
		if script.Parent.Commander:children()~=nil or script.Parent.Commander:children()>0 then
			local ch=script.Parent.Commander:children()
			for i=1, #ch do
				if string.lower(ch[i].Name)==string.lower(Player.Name) then
					Player.Chatted:connect(function(msg, recipient) onChatted(msg, recipient) end) 
				end
			end
		elseif string.lower(script.Parent.Commander.Value)==string.lower(Player.Name) then
			Player.Chatted:connect(function(msg, recipient) onChatted(msg, recipient) end) 
		end
	else
		Player.Chatted:connect(function(msg, recipient) onChatted(msg, recipient) end) 
	end
end

game.Players.ChildAdded:connect(onPlayerEntered)